5

I am using ddply right now. It's very easy to deal with numbers. Such as take the mean or standard deviation of the subgroup.

But I have difficulty dealing with strings. I would like to combine the strings in the same column in each subgroup, but I could not do it. I tried cbind paste, etc. Anyone can offer some help?

joran
  • 169,992
  • 32
  • 429
  • 468
user1652926
  • 85
  • 2
  • 5
  • 2
    Without a concrete, reproducible example, no, we probably can't offer much help. – joran Sep 06 '12 at 20:56
  • Please produce a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to get help... – ptocquin Sep 06 '12 at 20:57

2 Answers2

8

Add collapse="" to your paste statement

ddply(mtcars, .(carb), summarize, cyl_concatenated = paste(cyl, collapse = ""))
#  carb cyl_concatenated
#1    1          4664444
#2    2       8444888444
#3    3              888
#4    4       6686688888
#5    6                6
#6    8                8
Maiasaura
  • 32,226
  • 27
  • 104
  • 108
Dason
  • 60,663
  • 9
  • 131
  • 148
5

I see Dason has an approach. I would rather keeps separate things separate and would suggest:

 ddply(mtcars, .(carb), summarize, cyl_list = list(as.character(cyl)))
#-----------
  carb                     cyl_list
1    1          4, 6, 6, 4, 4, 4, 4
2    2 8, 4, 4, 4, 8, 8, 8, 4, 4, 4
3    3                      8, 8, 8
4    4 6, 6, 8, 6, 6, 8, 8, 8, 8, 8
5    6                            6
6    8                            8

You could also modify Dason's to use collapse=", " which would look the same as above but would have a different structure. With list() you need to convert to character when using that example, or you get the integer coding of the factor variable.

IRTFM
  • 258,963
  • 21
  • 364
  • 487