Home > Net >  Specific column selection from data.table in R
Specific column selection from data.table in R

Time:03-15

I have a data.table in R. The table has column names. I have a vector named colnames with a few of the column names in the table.

colnames<-c("cost1", "cost2", "cost3")

I want to select the columns whose names are in the vector colnames from the table. The name of the table is dt.

I have tried doing the following:

selected_columns <- dt[,colnames]

But this, does not work, and I get an error.
However, when I try the following, it works:

selected_columns <- dt[,c("cost1", "cost2", "cost3")]

I want to use the vector variable (colnames) to access the columns and not the c("..") method.
How can I do so?

CodePudding user response:

You can try like this:

dt[, .SD, .SDcols = colnames]

CodePudding user response:

dt<-data.frame(c(1:5), 3, 7, 6, 9)
colnames(dt)<-c('cost1', 'costt', 'cost2', 'cosss', 'cost3')
colnames<-c("cost1", "cost2", "cost3")
selected_columns <- dt[,colnames]

As an example... But I would choose another name than 'colnames'...

CodePudding user response:

Another nice alternatives is leveraging select from tidyverse/dplyr universe. select gives you a lot of flexibility when selecting columns from a data frame / tibble or data table.

library("data.table")
library("tidyverse")
df <- data.table(mtcars)
columns_to_select <- c("cyl", "mpg")
select(df, columns_to_select)

You can also skip quoting column names if you wish

select(df, c(cyl, mpg))

or leverage ellipsis and pass multiple quoted or unquoted names

Here, comparing multiple objects.

objs <- list(select(df, c(cyl, mpg)),
             select(df, cyl, mpg),
             select(df, "cyl", "mpg"))
outer(objs, objs, Vectorize(all.equal))

You may want to have a further look at dtplyr, which provides a bridge between data table and dplyr, if you want to go this route.

  • Related