Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organize subgroup strings (text)

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!

like image 974
Zhiqiang Wang Avatar asked Dec 01 '25 23:12

Zhiqiang Wang


1 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)
like image 142
Khaynes Avatar answered Dec 04 '25 12:12

Khaynes