Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can you use .SDcols to subset rows in data.table

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')]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading