如何将数据帧从DF1分组并汇总到DF4?我希望以我的字符串连接的方式传播数据。我可以使用
进入DF3DF1 <- 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
答案 0 :(得分:1)
而不是count
,使用summarise
将Owner
粘贴为 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