Search code examples
rdataframetidyverse

paste together first and last value by group


I have a df that looks like this:

group sequence link 
90      1      11|S1
90      2      10|S1
90      3      12|10
91      1      9|10
91      2      13|9
93      1      15|20
...

How can I store the first and last value of the linkvariable in each group as a new variable? Desired output is:

group sequence link  Key
90      1      11|S1 11|S1, 12|10
90      2      10|S1 11|S1, 12|10
90      3      12|10 11|S1, 12|10
91      1      9|10  9|10, 13|9
91      2      13|9 9|10,13|9
93      1      15|20 
....

Solution

  • You could do:

    library(dplyr)
    
    df %>%
      group_by(group) %>%
      mutate(
        Key = paste(link[1], link[n()], sep = ", ")
      )
    

    Though that wouldn't match your desired output. In your example data frame, you have e.g. the group 91 where there's only 1 value. The above code would give you 9|10 repeatedly both as beginning and end.

    If you'd like to only display one value in such cases, you can do:

    df %>%
      group_by(group) %>%
      mutate(
        Key = case_when(
          n() > 1 ~ paste(link[1], link[n()], sep = ", "), 
          TRUE ~ as.character(link)
          )
      )