将数据框(第一列是元素,第二列是它所属的类)转换为类列表

时间:2021-02-03 11:06:05

标签: r list dataframe

我有以下数据框:

df=data.frame(term=c("hello","affirms","allows","hello","always","allows","allows","affirms"),
       class=c("class 1","class 1","class 2", "class 2", "class 2","class 3","class 4","class 
4"),stringsAsFactors = FALSE)
df
     term   class
1   hello class 1
2 affirms class 1
3  allows class 2
4   hello class 2
5  always class 2
6  allows class 3
7  allows class 4
8 affirms class 4

我想获得这样的列表:

combinations <- list(
  hello   = c("class 1", "class 2"),
  affirms = c("class 1", "class 4"),
  allows  = c("class 2", "class 3", "class 4"),
  always  = c("class 2")
)

Split data.frame based on levels of a factor into new data.frames 中提供的解决方案不适合我的问题,因为如果我应用了上述问题的已接受答案:

X <- split(df, df$class)
Y <- lapply(seq_along(X), function(x) as.data.frame(X[[x]])[, 1]) 
names(Y) <- c("class 1", "class 2", "class 3", "class 4")
list2env(Y, envir = .GlobalEnv)
`class 1`

我得到

"hello"   "affirms"

这不是想要的结果。反正我是这样修改代码的:

X <- split(df, df$term)
Y <- lapply(seq_along(X), function(x) as.data.frame(X[[x]])[, 2]) 

结果似乎接近想要的结果,但是:

Y
[[1]]
[1] "class 1" "class 4"

[[2]]
[1] "class 2" "class 3" "class 4"

[[3]]
[1] "class 2"

[[4]]
[1] "class 1" "class 2"

我怎么知道,例如,Y[[4]] 对应于 hello,而 Y[[2]] 对应于 allows

2 个答案:

答案 0 :(得分:1)

使用split

split(df$class, df$term)

#$affirms
#[1] "class 1"   "class 4"

#$allows
#[1] "class 2" "class 3" "class 4"

#$always
#[1] "class 2"

#$hello
#[1] "class 1" "class 2"

答案 1 :(得分:1)

我们可以使用group_split

library(dplyr)
df %>%
    group_split(term)