Subset of all levels of one factor

Is there a way to subset all levels of one factor in one clean sweep?

Happening. Assuming you have a data frame in which one of the columns is a factor (data factor), and you want to create subsets of data that contain only one level of factor. This is easy to do when there are a small number of factors by writing individual subset commands. However, what if you have a large number of levels (e.g. 50+ levels)? Is there a command or a smart way to create all subsets in this case without having to write 50+ subset commands?

+6
source share
2 answers

Without creating a loop, the SPLIT function is the key to solving this problem.

Assuming that the factor column that you want to multiply (or a subgroup) is in the “coefficient” column of the data data “data”:

subsets<-split(data, data$factor, drop=TRUE) 

This will create a list of subsets based on the coefficient value. The list will have the same length as the number of factors.

If you need to put each subset in a separate data frame, you can access them by following these steps:

 group1<-subsets[[1]] group2<-subsets[[2]] ... 
+12
source

You can create a cycle based on the requested factor values ​​as follows:

 vals <- sort (unique (data[[factor]])) for (i in 1:length(vals)) { subset <- (data[[factor]]==vals[i]) n <- length (data[[factor]][(subset)]) if (n >= min.n) { ... } } 
0
source

Source: https://habr.com/ru/post/950451/


All Articles