Search code examples
rdataframedcast

Transpose R data frame and concatenate values that occur in multiple columns


I need to transform a data frame in R from something like this:

id    past     present  future
id1   A        A        B
id2   B                 C
id3   A        C        
id4   B         B        A

To this:

id    A              B              C
id1   past, present  future
id2                  past           future
id3   past                          present
id4   future         past, present

I have tried playing around with dcast but I'm pretty new to R and haven't been able to get anything close to what I need. Should I be using something else?

Thanks!


Solution

  • We can gather it to 'long' format, grouped by 'id', 'val', paste the 'key' elements together and spread it to 'wide'

    library(tidyverse)
    gather(df1, key, val, -id) %>%
            filter(val !="") %>% 
            group_by(id, val) %>% 
            summarise(key = toString(key)) %>% 
            spread(val, key, fill = "")
    # A tibble: 4 x 4
    # Groups:   id [4]
    #     id             A             B       C
    # * <chr>         <chr>         <chr>   <chr>
    #1   id1 past, present        future        
    #2   id2                        past  future
    #3   id3          past               present
    #4   id4        future past, present