Home > other >  Can you use .SDcols to subset rows in data.table
Can you use .SDcols to subset rows in data.table

Time:11-04

Is there a way to use .SDcols in datatable to select rows. For example, in mtcars, select rows where vs, am and carb all equal 1. I have a lot of columns and want to avoid a lot of typing.

I know this does not work

 mtcars[ lapply( .SD == 1),  , .SDcols = c('vs, 'am', 'carb')]

CodePudding user response:

We may need Reduce to return a single logical vector and use that as index for subsetting

library(data.table)
mtcars[mtcars[, Reduce(`&`, lapply( .SD, `==`,  1)), 
         .SDcols = c('vs', 'am', 'carb')]]

-output

    mpg cyl  disp hp drat    wt  qsec vs am gear carb
1: 22.8   4 108.0 93 3.85 2.320 18.61  1  1    4    1
2: 32.4   4  78.7 66 4.08 2.200 19.47  1  1    4    1
3: 33.9   4  71.1 65 4.22 1.835 19.90  1  1    4    1
4: 27.3   4  79.0 66 4.08 1.935 18.90  1  1    4    1

Or use rowSums to create the logical vector

mtcars[mtcars[, .I[rowSums(.SD == 1) == 3], .SDcols = c('vs', 'am', 'carb') ]]

data

mtcars <- as.data.table(mtcars)
  • Related