In R, how to split a data into list of multiple subsets based on multiple categorical variables? -


in data frame, have lot of logical variables , want split data frame multiple subsets conditional on each logical variable true. example, let's suppose df:

     v1    v2    v3 v4 1  true  true false  2 2  true false  true  5 3 false  true false  4 

so want have 3 subsets:

[1]      v1    v2    v3 v4 1  true  true false  2 2  true false  true  5  [2]      v1    v2    v3 v4 1  true  true false  2 2 false  true false  4  [3]      v1    v2    v3 v4 1  true false  true  5 

thanks help!

a simple lapply loop should trick:

read.table(textconnection("v1 v2 v3 v4 t  t  f  2 t  f  t  5 f  t  f  4"), header=t) -> df  lapply(1:(ncol(df)-1), function(i) {     subset(df, df[[i]]) })  [[1]]     v1    v2    v3 v4 1 true  true false  2 2 true false  true  5  [[2]]      v1   v2    v3 v4 1  true true false  2 3 false true false  4  [[3]]     v1    v2   v3 v4 2 true false true  5 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -