0

I have the following data frame with 3 columns

category <- c("A", "A", "A", "B","B")
id <- c(1,1,2,3,3)
text <- c("abc", "def", "ghi", "jkl", "pqr")
df <- data.frame(category,id,text)

> df
category id text
1        A  1  abc
2        A  1  def
3        A  2  ghi
4        B  3  jkl
5        B  3  pqr

I want to concatenate the text per id per group

My output needs to be like:

A   1   "abc def"
A   2   "ghi"
B   3   "jkl pqr"

I tried using

library(stringr)
str_c(df[,3], collapse = NULL)

But my output is not correct, also how can I get this per id per group

Hardik Gupta
  • 4,700
  • 9
  • 41
  • 83

1 Answers1

3

Using dplyr, you can do:

library(dplyr)
df %>% group_by(category,id) %>% summarise(text=paste(text,collapse=" "))

  category    id    text
    <fctr> <dbl>   <chr>
1        A     1 abc def
2        A     2     ghi
3        B     3 jkl pqr
Lamia
  • 3,845
  • 1
  • 12
  • 19