Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select columns in data.table based on logical vector

Let's say I have the following data.frame and the following data.table:

DF = data.frame(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)

With a data.frame, I can select columns based on a logical vector as follows:

DF[,c(TRUE,TRUE,FALSE)]

The result is:

  x y
1 a 1
2 a 3
3 a 6
4 b 1
5 b 3
6 b 6
7 c 1
8 c 3
9 c 6

However

DT[,c(TRUE,TRUE,FALSE)]

leads to:

[1]  TRUE  TRUE FALSE

How can it be done?

like image 588
EDC Avatar asked Oct 23 '15 12:10

EDC


1 Answers

Update 2020-04-22

In current CRAN version of data.table, DT[ , c(TRUE, TRUE, FALSE)] would work -- no need for with=FALSE. Leaving this older answer here for posterity:


We need with=FALSE

DT[, c(TRUE, TRUE, FALSE), with=FALSE]

Based on the documentation in ?data.table

By default with=TRUE and j is evaluated within the frame of x; column names can be used as variables. When with=FALSE j is a character vector of column names or a numeric vector of column positions to select, and the value returned is always a data.table. with=FALSE is often useful in data.table to select columns dynamically.

like image 104
akrun Avatar answered Sep 30 '22 23:09

akrun