Organize subgroup strings (text)

Viewed 66

I am trying to convert something like this df format:

df <- data.frame(first = c("a", "a", "b", "b", "b", "c"), 
  words =c("about", "among", "blue", "but", "both", "cat"))

df
  first words
1     a about
2     a among
3     b  blue
4     b   but
5     b  both
6     c   cat

into the following format:

df1
  first           words
1     a    about, among
2     b blue, but, both
3     c             cat
> 

I have tried

aggregate(words ~ first, data = df, FUN = list)

  first   words
1     a    1, 2
2     b 3, 5, 4
3     c       6

and tidyverse:

df %>%
  group_by(first) %>% 
  group_rows()

Any suggestions would be appreciated!

2 Answers

A data.table solution:

library(data.table)

df <- data.frame(first = c("a", "a", "b", "b", "b", "c"), 
  words =c("about", "among", "blue", "but", "both", "cat"))

df <- setDT(df)[, lapply(.SD, toString), by = first]

df
#    first           words
# 1:     a    about, among
# 2:     b blue, but, both
# 3:     c             cat

# convert back to a data.frame if you want
setDF(df)

Using tidyverse, after the group_by use summarise to either paste

library(dplyr)
df %>% 
  group_by(first) %>%
  summarise(words = toString(words))
# A tibble: 3 x 2
#  first words          
#  <fct> <chr>          
#1 a     about, among   
#2 b     blue, but, both
#3 c     cat           

or keep it as a list column

df %>%
  group_by(first) %>%
  summarise(words = list(words))
Related