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

javascript - Confirm a form & display message if form is valid with JQuery -

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -