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

时间:2018-01-23 03:01:19

标签: r dplyr aggregate summarize

如何将数据帧从DF1分组并汇总到DF4?我希望以我的字符串连接的方式传播数据。我可以使用

进入DF3

enter image description here 例如:

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

1 个答案:

答案 0 :(得分:1)

而不是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