我已经从该数据帧structure(list(ï..Name = structure(c(5L, 5L, 12L, 6L, 10L, 13L,
7L, 1L, 4L, 2L, 9L, 8L, 3L, 11L), .Label = c("Alex", "Alice",
"Greg", "Jay", "Jim", "Mick", "Mike", "Patrick", "Reese", "Sarah",
"Simon", "Sue", "Timmy"), class = "factor"), Partner = structure(c(7L,
9L, 7L, 11L, 10L, 2L, 1L, 1L, 4L, 8L, 5L, 6L, 3L, 3L), .Label = c("Alan",
"Alex", "Britney", "Dalton", "Jeff", "Mary", "Peggy", "Percy",
"Sue", "Susan", "Tara"), class = "factor"), Group1 = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("a",
"b", "c", "d", "e", "f", "g"), class = "factor"), Group2 = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("Alpha",
"Bravo ", "Charlie", "Delta", "Echo", "Foxtrot", "Gamma"), class = "factor")), class = "data.frame", row.names = c(NA,
-14L))
中创建了一个数据帧列表
list_df=df %>%
split(group_indices_(df, .dots =c("Group1","Group2")))
使用此代码
Group1
我首先尝试通过结合Group2
和list_df
中的因子来命名每个数据框对象。然后,我尝试对Name
中的每个数据帧进行子集化,以仅在第1列中包含Partner
列,在第2列中包含sub_toy=lapply(list_df, select,ï..Name, Partner)
。
我已经能够通过这样做对数据帧进行子集
{{1}}
我不知道的是如何在子集之前实际命名内部对象列表,以便知道每个数据框属于哪个组组合
答案 0 :(得分:1)
您正在寻找以下任何一个
split(df[1:2], df[3:4], TRUE)
split(df[1:2], do.call(paste, df[3:4]))
答案 1 :(得分:0)
这是我想出的快速解决方案。希望能得到您想要的东西:
n <- names(list_df)
for(ii in 1:length(list_df)) {
n[ii] = paste(as.character(unique(list_df[[ii]][[3]])),
"_",
as.character(unique(list_df[[ii]][[4]])),
sep = "")
}
names(list_df) <- n
l <- lapply(list_df, function(x) {x[,-c(3,4)]}) #or your function call, too