Let's say I have a factor in which there are tons of levels. And I grouped some of those levels that are represented by the grps variable.
I can use βsplitβ to split my data frame, but is it possible to split the data frame so that the merged layers represented by grps are in the same split?
set.seed(42)
fooLevels <- c(115,119,156,120,158,219)
foo <- fooLevels[round(runif(20, min=1, max=6))]
doo <- rnorm(20)
df <- data.frame(foo,doo)
grps <- c("{115}","{119}","{156}{120}{158}{219}")
splits <- split(df, f = df$foo)
I would like the result to look like this:
>splits
$`{115}`
foo doo
8 115 0.08983289
9 115 -2.99309008
12 115 0.18523056
$`{119}`
foo doo
2 119 -0.7838389
7 119 0.6792888
13 119 0.5818237
$`{156}{120}{158}{219}`
foo doo
1 120 0.3219253
4 120 0.6428993
6 120 0.2765507
11 120 -0.3672346
18 120 1.0385061
20 120 0.7208782
3 156 1.5757275
10 156 0.2848830
17 156 0.3358481
5 158 0.08976065
16 158 1.30254263
19 158 0.92072857
14 219 1.3997368
15 219 -0.7272921
The order of the lines in the list (data.frame) is irrelevant.
source
share