I have a dataframe with columns A and B where B is a character string I want to groupby column A and the combine the elements of B into a string where I can then get the unique elements of that grouped characters (using R 4.0.5 and dplyr
).
Example:
df <- data.frame(a = c("a","a","a","b","b","b"), c = c("d","d","d","d","e","e"))
df.new <- df %>%
group_by(a) %>%
summarise(new_strs = c(c))
which outputs this data.frame
:
1 a d
2 a d
3 a d
4 b d
5 b e
6 b e
Desired DF output:
1 a c("d","d","d")
6 b c("d","e","e")
How can I change the code to get the result. I also tried paste(x, collapse = ",")
but it gives a single string rather then a group of strings.
CodePudding user response:
My first thought is to use list
rather than c
exampledf <- data.frame(a = c("a","a","a","b","b","b"), c = c("d","d","d","d","e","e"))
exampledf %>%
group_by(a) %>%
summarise(new_strs = list(c))
CodePudding user response:
We may use aggregate
from base R
aggregate(c ~ a, df, list)
a c
1 a d, d, d
2 b d, e, e