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"
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With