Aeyxen
Aeyxen

Reputation: 23

Combining multiple rows into one single row in Dataframe in R

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?

Upvotes: 0

Views: 128

Answers (1)

NiklasvMoers
NiklasvMoers

Reputation: 329

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).

Upvotes: 1

Related Questions