Sara U
Sara U

Reputation: 67

r aggregate or collapse specific column values by id

This may be a duplicate questions. If so, then my apologies, I can delete this question. What I have is a dataset like this

  ID   Color   Brand  Points
  1    Blue    Bunny  10
  1    Red     Bunny  11
  2    Pink    Robin  12
  2    Red     Robin  13

What I am trying to do is just collapse/aggregate values from column : Color, Points. The final dataset to look like this

  ID   Color       Brand  Points
  1    Blue,Red    Bunny  10, 11
  2    Pink, Red   Robin  12, 13

Any suggestions on collapsing these columns is much appreciated. Thanks.

Upvotes: 2

Views: 259

Answers (1)

akrun
akrun

Reputation: 886948

We can group by 'ID', 'Brand' and paste the other columns

library(dplyr)
df1 %>%
     group_by(ID, Brand) %>%
     summarise(across(everything(), toString))

Upvotes: 2

Related Questions