Home > Software engineering >  Storing all entries of an ID in a list within a column in tidyverse
Storing all entries of an ID in a list within a column in tidyverse

Time:07-02

So let's say I have some simple data as follows:

ID   value  character
001      A        JABA
002      B        FABA
001      B        RABA
003      D        RIBI
003     TT        LENI
004      A        LENT
001      A        TATA
004      N        YAYA
004      N        YANA

And what I want to do (using tidyverse/dplyr) is to create a summarized table of these IDs and their values across the entire table. If the user has duplicates of a value, it should only appear once.

Expected Output

ID   values
001  [A,B]
002  [B]
003  [D,TT]
004  [A,N]

Not overly attached to format but you get the idea :)

CodePudding user response:

library(dplyr)
df %>%
  group_by(ID) %>%
  distinct(value) %>%
  summarize(val = paste(value, collapse = ", "))

# A tibble: 4 × 2
     ID val  
  <int> <chr>
1     1 A, B 
2     2 B    
3     3 D, TT
4     4 A, N 
  • Related