0

How do I group and summarise a dataframe from DF1 to DF4? I want to spread the data in such a way that my strings are concatenated. I am able to get to DF3 using

enter image description here Example:

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
val
  • 1,629
  • 1
  • 30
  • 56
  • Your `DF1` and the `DF1` from the image are different. What exactly do you want? – www Jan 23 '18 at 03:05

1 Answers1

1

Instead of count, use summarise to paste Owner as string per nWins-Type, then 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        
Psidom
  • 209,562
  • 33
  • 339
  • 356