1
votes

I have a problem with the summarise function inside group_by, of the package dplyr.

This is how the dataframe looks like:

df <- read.table(text="id groups times strings
1   1      a     1     xxx
2   2      a     2     zzz
3   3      a     1     yyy
4   4      b     1     xxx
5   5      b     1     yyy
6   6      b     1     xxx
7   7      c     3     yyy
8   8      c     1     xxx
9   9      c     1     xxx
10 10      c     1     yyy", stringsAsFactors=FALSE)

I need to group_by the variable "groups" to obtain a dataframe that looks like this:

view(group.df)
  id2 groups2                            c.string
1   1       a             'xxx','zzz','zzz','yyy'
2   2       b                   'xxx','yyy','xxx'
3   3       c 'yyy','yyy','yyy','xxx','xxx','yyy'


str(group.df)
'data.frame':   3 obs. of  3 variables:
 $ id2     : int  1 2 3
 $ groups2 : chr  "a" "b" "c"
 $ c.string: chr  "'xxx','zzz','zzz','yyy'" "'xxx','yyy','xxx'" 
"'yyy','yyy','yyy','xxx','xxx','yyy'"

My problem is that I don't know how to code the summarise function nested in group_by to multiply the string variable "df$strings" by the value of "df$times", only when "df$times" is >1.

Thank you for your help.

1

1 Answers

0
votes

Seems like you are just ignoring the id column, so this should work

df %>% 
  group_by(groups) %>% 
  summarize(c.string=paste(sQuote(rep(strings, times)), collapse=","))
#   groups                            c.string
#   <chr>                               <chr>
# 1      a             ‘xxx’,‘zzz’,‘zzz’,‘yyy’
# 2      b                   ‘xxx’,‘yyy’,‘xxx’
# 3      c ‘yyy’,‘yyy’,‘yyy’,‘xxx’,‘xxx’,‘yyy’