0

I am currently working with a data set has this feature:

enter image description here

however what I want it to be like is:

30 | 2,5,3

Anyone know how to do it ?

phiver
  • 23,048
  • 14
  • 44
  • 56
Kai.L
  • 3
  • 3

2 Answers2

0

We can use aggregate() with paste().

df <- data.frame(x = rep(30, 3), y = c(2,5,3))
> df
   x y
1 30 2
2 30 5
3 30 3

> aggregate(y ~ x, data = df, paste, collapse = ",")
   x     y
1 30 2,5,3
LAP
  • 6,605
  • 2
  • 15
  • 28
  • Exactly what I want!!! Easy and Sufficient! I also wonder what type of "2,5,3" is now? – Kai.L May 24 '18 at 09:18
  • The class should now be `character`. Because of the comma, any kind of `numeric` class is not possible. – LAP May 24 '18 at 09:26
0

Try this:

library(dplyr)
my_df <- data_frame(A = 30, B = c(2, 5, 3))
my_transformed_df <- my_df %>%
    group_by(A) %>%
    summarise(B = paste(B, collapse = ", "))
AlexR
  • 2,412
  • 16
  • 26