请考虑以下数据:
myd <- dput(myd)
structure(list(group = c("g1", "g1", "g1", "g1", "g1", "g2",
"g2", "g2", "g2", "g3", "g3", "g3", "g3", "g3"), X1 = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0), X2 = c(1, 1, 1, 1, 1, 0,
0, 0, 0, 1, 0, 0, 2, 0), X3 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1, 1, 2, 1), X4 = c(1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 2, 1),
X5 = c(1, 0, 0, 0, 0, 2, 2, 2, 2, 2, 0, 1, 2, 2), X6 = c(2,
2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 2, 2), X7 = c(2, 2, 2, 2,
2, 2, 2, 2, 1, 1, 0, 2, 2, 2), X8 = c(1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 2, 1), X9 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1, 1, 0, 2), X10 = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 2,
1, 2), X11 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 2, 2),
X12 = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 0, 1, 0, 1, 2), X13 = c(2,
2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 0, 2), X14 = c(0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1), X15 = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0), X16 = c(1, 1, 0, 0, 0, 1, 1,
1, 1, 1, 0, 2, 1, 1), X17 = c(2, 2, 2, 2, 2, 1, 1, 1, 1,
1, 0, 2, 1, 1), X18 = c(2, 2, 2, 2, 2, 1, 1, 1, 1, 0, 1,
1, 0, 1), X19 = c(2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 1, 0, 1,
0), X20 = c(2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 1, 1, 0, 0), X21 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1), X22 = c(0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 0, 2, 0, 1), X23 = c(1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 2, 0, 1, 1), X24 = c(1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 1, 2, 1), X25 = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 2, 0), X26 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 2, 1)), row.names = c("S1", "S2", "S3", "S4", "S5", "S6",
"S7", "S8", "S9", "S10", "S11", "S12", "S13", "S14"), class = "data.frame")
我看起来像这样:
group X1 X2 X3 X4
S1 g1 0 1 1 1
S2 g1 0 1 1 1
S3 g1 0 1 1 1
S4 g1 0 1 1 0
S5 g1 0 1 1 0
我想基于myd$group
的唯一值3(g1,g2,g3)创建一个数据帧列表,因此列表的每个元素都是myd数据帧的子集,其唯一值{ {1}}。我知道如何使用for循环来做到这一点,但我认为R中的for循环很慢,如果我错了,请纠正我。因此,非常欢迎带有某些应用系列或dplyr软件包的解决方案。
答案 0 :(得分:3)
我们可以使用split
创建list
个data.frame
lst1 <- split(myd, myd$group)
在tidyverse
中,可以是
library(dplyr)
myd %>%
group_split(group)