2

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?

2
  • Could you send your dt? Because with a dt that I made, it works for me. Commented Mar 15, 2022 at 7:41
  • Does tis answer your question : stackoverflow.com/a/12392269/13513328 Commented Mar 15, 2022 at 10:05

2 Answers 2

2

You can try like this:

dt[, .SD, .SDcols = colnames]

Meanwhile, data.table gives an alternative choice in recent version:

dt[, ..colnames]
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.