使用 dplyr 管道在 R 中连接时聚合

Aggregating while concatenating in R using dplyr piping

如何对从 DF1 到 DF4 的数据帧进行分组和汇总?我想以连接字符串的方式传播数据。我可以使用

访问 DF3

示例:

DF1 <- data.frame(Owner = c("Owner A","Owner B","Owner C","Owner B","Owner D"),
                         Project = c("project AA","project BA","project CA","project BB","project DA"),
                         nWins = c(1,4,4,2,1),
                         Type = c("A","B","B","C","A"))
DF1 %>% group_by(nWins, Type) %>% count %>% spread(Type,n) # to DF3

而不是 count,使用 summariseOwner 粘贴为每个 nWins-Type 的字符串,然后 spread

DF1 %>% 
    group_by(nWins, Type) %>% 
    summarise(Owner = paste(Owner, collapse=";")) %>% 
    spread(Type, Owner, fill="") %>% 
    as.data.frame()

#  nWins               A               B       C
#1     1 Owner A;Owner D                        
#2     2                                 Owner B
#3     4                 Owner B;Owner C