Home > OS >  How to aggregate the rows with text grouping them by date in R?
How to aggregate the rows with text grouping them by date in R?

Time:04-09

To give an example, I have a table like this one: enter image description here

But I would like to have a table like this one: enter image description here

CodePudding user response:

Do you need this? group_by with summarise and toString():

library(dplyr)
# your dataframe
Date <- c("30/05/2020", "30/05/2020", "30/05/2020",
          "29/05/2020", "29/05/2020")
Text <- c("Here we are", "He likes ABC", "She likes DEF",
          "Cat eats XYZ", "I have a pet")

df <- data.frame(Date, Text)


df %>% 
  group_by(Date) %>% 
  summarise(Text = toString(Text))

output:

  Date       Text                                    
  <chr>      <chr>                                   
1 29/05/2020 Cat eats XYZ, I have a pet              
2 30/05/2020 Here we are, He likes ABC, She likes DEF
  • Related