0

I have a table of data and I want to group by 2 columns. The issue I have is that for one of the columns, when I group them, I want to create a list of the elements in one of the columns but not the other. I am using dplyr.

This is an example of what I have

| col1 | col2 |
|  x   |   1  |
|  x   |   2  |
|  x   |   3  |
|  y   |   4  |
|  y   |   5  |
|  z   |   6  |

and I want to group to this:

| col1 | col2   | 
|  x   | 1, 2, 3|
|  y   |  4, 5  |
|  z   |   6    |

I have already tried grouping by both columns but that creates a tibble within the second column and I want to format it into a list.

AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
blub_9
  • 87
  • 8

1 Answers1

1

Assuming your data name is df

library (dplyr) 
df  %>%  group_by(col1)  %>%  summarise(col2  = toString(col2)) 
AnilGoyal
  • 25,297
  • 4
  • 27
  • 45