What is the fastest way to perform concate-like operation over a data.frame
in R
? Suppose I have the following table:
df <- data.frame(content = c("c1", "c2", "c3", "c4", "c5"),
groups = c("g1", "g1", "g1", "g2", "g2"),
stringsAsFactors = F)
df$groups <- as.factor(df$groups)
I want to concate the content of cells in content
column, by groups, efficiently, to receive the equivalent to:
df2 <- data.frame(content = c("c1 c2 c3", "c4 c5"),
groups = c("g1", "g2"),
stringsAsFactors = F)
df2 $groups <- as.factor(df2 $groups)
I would prefer some dplyr
operation, but have no good idea how to apply it.