Create new variable appending strings comming from other variable which have in common the same code

I there, I´d like to mutate new column appending the data (strings) from other variable which have in common the same code.

Input:
a<- data.frame(id= c(1,1,1,2,2,3,3,3,3), description = c("roberto", "alberto", "laura", "rafael", "luisa", 
                                                         "alex", "sergio", "mateo", "jose"))

                                   
Output:

a1<- data.frame(id= c(1,1,1,2,2,3,3,3,3), description = c("roberto", "alberto", "laura", "rafael", "luisa", 
                                                          "alex", "sergio", "mateo", "jose"), 
                new = c( "roberto alberto laura","roberto alberto laura","roberto alberto laura",
                         "rafael luisa","rafael luisa",
                         "alex sergio mateo jose","alex sergio mateo jose","alex sergio mateo jose","alex sergio mateo jose" )
)

Do you know some way to make it possible?
Many thanks

Here is a very simple way to do it with aggregate

library(stats)

a<- data.frame(id= c(1,1,1,2,2,3,3,3,3), description = c("roberto", "alberto", "laura", "rafael", "luisa", 
                                                         "alex", "sergio", "mateo", "jose"))


b <- stats::aggregate(a$description, list(a$id), paste, collapse=" ")

b
#>   Group.1                      x
#> 1       1  roberto alberto laura
#> 2       2           rafael luisa
#> 3       3 alex sergio mateo jose

Created on 2022-02-14 by the reprex package (v2.0.0)

(There exists a lot of ways to now rename etc.

1 Like

Fantastic!, thank you very much!

1 Like

Feel free to mark it as the solution if it solved your problem :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.