1

I have the dataframe df1. I need to concatenate row elements in X2 by similar X1 elements. Basically, if the values in df1 are the same, I need to concatenate the letters in X2. The output should look something like df2.

df1 <- data.frame(c(1, 1, 1, 2, 2, 2, 2, 3, 3, 4), 
                  c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"))
names(df1) <- c("X1", "X2")
df2 <- data.frame(c(1, 2, 3, 4), c("A, B, C", "D, E, F, G", "H, I", "J"))
names(df2) <- names(df1)
user13589693
  • 369
  • 1
  • 8

1 Answers1

0

We can use aggregate

aggregate(X2 ~ X1, df1, toString)
akrun
  • 874,273
  • 37
  • 540
  • 662