使用dplyr中的c()汇总字符串汇总或聚合

时间:2014-12-04 07:53:34

标签: r string aggregate dplyr

我想在dplyr中使用c()作为聚合函数来聚合一些字符串。我首先尝试了以下内容:

> InsectSprays$spray = as.character(InsectSprays$spray)
> dt = tbl_df(InsectSprays)
> dt %>% group_by(count) %>% summarize(c(spray))
Error: expecting a single value

但是在aggregate()中使用c()函数可以工作:

> da = aggregate(spray ~ count, InsectSprays, c)
> head(da)
  count                  spray
1     0                   C, C
2     1       C, C, C, C, E, E
3     2             C, C, D, E>

在stackoverflow hinted中搜索而不是c()函数,使用带崩溃的paste()可以解决问题:

dt %>% group_by(count) %>% summarize(s=paste(spray, collapse=","))

dt %>% group_by(count) %>% summarize(paste( c(spray), collapse=","))

我的问题是:为什么c()函数在aggregate()中工作但在dplyr summary()中不起作用?

1 个答案:

答案 0 :(得分:5)

如果仔细观察,我们会发现c()在我们使用do()时确实有效(在某种程度上)。但据我了解,dplyr目前不允许这种类型的列表打印

> InsectSprays$spray = as.character(InsectSprays$spray)
> dt = tbl_df(InsectSprays)
> doC <- dt %>% group_by(count) %>% do(s = c(.$spray))
> head(doC)
Source: local data frame [6 x 2]

  count        s
1     0 <chr[2]>
2     1 <chr[6]>
3     2 <chr[4]>
4     3 <chr[8]>
5     4 <chr[4]>
6     5 <chr[7]>

> head(doC)[[2]]
[[1]]
[1] "C" "C"

[[2]]
[1] "C" "C" "C" "C" "E" "E"

[[3]]
[1] "C" "C" "D" "E"

[[4]]
[1] "C" "C" "D" "D" "E" "E" "E" "E"

[[5]]
[1] "C" "D" "D" "E"

[[6]]
[1] "D" "D" "D" "D" "D" "E" "E"