Search code examples
rdplyraggregatesummarize

Aggregating while concatenating in R using dplyr piping


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

Solution

  • 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