Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select columns in data.table using a character vector of certain column names? [duplicate]

Tags:

r

data.table

I am trying to select those columns in a data.table whose name appears in my character vector. The operation works in a pure data.frame, but doesn't work in a data.table. Here's a reproducible example.

> names(mtcars)  [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" [11] "carb" > myVector <- c('disp', 'hp', 'wt') > head(mtcars[, myVector])                   disp  hp    wt Mazda RX4          160 110 2.620 Mazda RX4 Wag      160 110 2.875 Datsun 710         108  93 2.320 Hornet 4 Drive     258 110 3.215 Hornet Sportabout  360 175 3.440 Valiant            225 105 3.460 

I just made a vector which includes disp, hp, and wt and I selected the corresponding columns in my data.frame using that vector. Let's now make a data.table object from my data.frame and try to do the same operation.

> library(data.table) > mtcarsDT <- data.table(mtcars) > mtcarsDT[, myVector] [1] "disp" "hp"   "wt"   
like image 517
coneyhelixlake Avatar asked Aug 24 '15 14:08

coneyhelixlake


People also ask

How do I select a column in a Datatable?

The colvis button type provides a columns option to allow you to select what columns should be included in the column visibility control list. This option is a column-selector and thus a number of methods to select the columns included are available including jQuery selectors and data index selectors.

How do I select a specific column in a Dataframe in R?

To select a column in R you can use brackets e.g., YourDataFrame['Column'] will take the column named “Column”. Furthermore, we can also use dplyr and the select() function to get columns by name or index. For instance, select(YourDataFrame, c('A', 'B') will take the columns named “A” and “B” from the dataframe.


1 Answers

We can use .. notation to find myVector as a vector of column positions, like it would work in data.frame

mtcarsDT[, ..myVector] 

According to ?data.table

In case of overlapping variables names inside dataset and in parent scope you can use double dot prefix ..cols to explicitly refer to 'cols variable parent scope and not from your dataset.

like image 167
akrun Avatar answered Sep 23 '22 18:09

akrun