Display Row Names in a Data.Table Object

Display row names in a data.table object

This is more or less verbatim from comments.

data.table doesn't support row names. This is intentional, as row names are a bad design choice, because they are far more cumbersome to use than columns (and especially so in data.table, where columns are so much easier to deal with than in data.frame) and are only a subset of what kind of data columns can represent (recall that row names in data.frame are a character vector only, whereas columns can be anything).

Reformat data tables based on row names to generate new columns in R

Here's another way to do it:

library(dplyr)
library(tidyr)
(wide <- reshape(df %>% add_rownames() %>% separate(rowname, c("rowname", "id")),
idvar = "rowname",
timevar = "id",
direction = "wide",
sep = ""))
# rowname m1st m2st m3st m1nd m2nd m3nd m1rt m2rt m3rt
# 1 P001 60.0 2.000 1 NA NA NA NA NA NA
# 2 P003 NA NA NA 14.30 2.077 1 29.6 2.077 1
# 4 P006 10.3 2.077 1 79.30 2.077 1 NA NA NA
# 6 P008 NA NA NA 9.16 2.077 1 NA NA NA

wide[is.na(wide)] <- 0
rownames(wide) <- wide[, 1]
wide$rowname <- NULL
wide
# m1st m2st m3st m1nd m2nd m3nd m1rt m2rt m3rt
# P001 60.0 2.000 1 0.00 0.000 0 0.0 0.000 0
# P003 0.0 0.000 0 14.30 2.077 1 29.6 2.077 1
# P006 10.3 2.077 1 79.30 2.077 1 0.0 0.000 0
# P008 0.0 0.000 0 9.16 2.077 1 0.0 0.000 0

filter row in data.table using object with same name as column

You could use get() and the env argument:

dt[y == get('y', env = -2)]
# x y
# 1: 3 c

Beware this makes certain assumptions about your setup. If your y is defined in the global environment as in your example, then a safer solution (as suggested by Ian Campell) would be:

dt[y == get('y', env = globalenv())]

For each row in data.table, find column name where value == X

You can use apply and which:

df <- data.frame( x1 = c(0, 0, 1), x2 = c(1, 0 , 0), x3 = c(0, 1 , 0) )
idx <- apply( df, 1, function(row) which( row == 1 ) )
cbind( df, Number = colnames( df[ , idx] ) )

x1 x2 x3 Number
1 0 1 0 x2
2 0 0 1 x3
3 1 0 0 x1

Using dataframe's first data column for row names

fread is part of the data.table package. When you import, it does so as a data.table. The reason you can't assign row names is that data.tables cannot have row names. It's an attribute of the package. See https://cran.r-project.org/web/packages/data.table/data.table.pdf.

Try using base or dplyr and you shouldn't have any trouble.

Also, see Display row names in a data.table object.

How to get Column Names using Linq from DataTable

The problem is Select() is projecting the objects into a new form. You are seeing 2018 because of '=' instead of '=='. You need to use Where()

 var linqTable = myTable.AsEnumerable().Where( x => x.Field<int>(0) == 2018);

You will still end up with a list of DataRows though. The DataTable object isn't really what you should be using because it already provides a nice way to filter its rows:

myTable.Rows.Find(2018);

If you are trying to convert it to a list of objects you should use the Select() method something like:

var linqTable = myTable.AsEnumerable().Where(x => x.Field<int>(0) == 2018)
.Select(x => new
{
year = x[0],
p1 = x[1],
p2 = x[2] // etc...
});

How do I get column names to print in this C# program?

You need to loop over loadDT.Columns, like this:

foreach (DataColumn column in loadDT.Columns)
{
Console.Write("Item: ");
Console.Write(column.ColumnName);
Console.Write(" ");
Console.WriteLine(row[column]);
}

How to assign row names to a reactive data frame in Shiny?

Try this

library(shiny)
library(DT)
library(datasets)

ui <- basicPage("",
DTOutput("table"),
DTOutput("head1"),
DTOutput("head2")
)

server <- function(input, output, session) {

df <- reactive({
df <- data.frame(v1=c("a", "b"), v2=c(10,20))
row.names(df) <- df[,1] # THIS WORKs
df
})

df1 <- reactive({ # THIS ALSO WORKs
data <- df()
row.names(data) <- df()[,1]
data
})

# Show data in a table ----
output$table <- renderDT({
datatable(
{df()},
filter = 'top',
class="cell-border stripe",
rownames = TRUE
) # end of datatable
})

output$head1 <- renderDT({
head(df())
})

output$head2 <- renderDT({
head(df1())
})

}

shinyApp(ui = ui, server = server)


Related Topics



Leave a reply



Submit