0

Suppose I have a dataframe, which looks like this.

| Category | Text |

| :--------: | :--------------------: |

| First | I am Groot. |

| First | We are Groot. |

| Second | The Dark Knight Rises. |

| Second | I am Batman. |

But we want to combine rows in column Text, which happens to have same value in category column, into one row and make it look like this.

| Category | Text |

| -------- | ------------------------------------ |

| First | I am Groot. We are Groot. |

| Second | The Dark Knight Rises. I am Batman. |

How do I do that?

Aeyxen
  • 23
  • 4
  • Would answers from [this question](https://stackoverflow.com/questions/38514988/concatenate-strings-by-group-with-dplyr) help you? – Ben Oct 24 '21 at 13:46

1 Answers1

1

data.table solution:

library(data.table)
dt0 <- data.table(
  Category = c(rep("First", 2), rep("Second", 2)),
  Text = c("I am Groot.", "We are Groot.", "The Dark Knight Rises.", "I am Batman.")
)
dt <- dt0[, .(Text = paste0(Text, collapse = " ")), by = .(Category)]
dt

Explanation: paste0 takes the column Text (which, in data.table syntax is evaluated to dt$Text) and collapses it to a single value. This calculation is performed for each unique value in Category, indicated by by = .(Category).

NiklasvMoers
  • 309
  • 2
  • 13