0

I got the following df and a bit stuck at the moment:

df1
X1    X2    X3    X4
A     1     a     x
A     2     a     x
A     3     a     y
B     1     b     x
...

and I am trying to aggregate the df so that I have a unique value IF X4 is the same so I want this:

df2
X1    X2    X3    X4
A     1,2   a     x
A     3     a     y
B     1     b     x
...

I tried this:

df2 <-aggregate(.~X1,df1,unique)

but it doesn't really work. X2 is also a numerical value, but I want it listed as if it were a character (transformed it into character).

Can someone help?

antonina
  • 109
  • 8

1 Answers1

0

We can paste the unique elements

aggregate(.~ X1, df1, function(x) toString(unique(x)))
akrun
  • 874,273
  • 37
  • 540
  • 662