Skip to content

Question about collapse/aggregate and avoidance of loops

3 messages · Bernd Weiss, PIKAL Petr, Patrick Burns

#
Dear all,

given the following data

## original data
id <- c(1,1,1,2,2,3)
author <- c("A","B","C","D","E","F")
tmp <- data.frame(id,author)
tmp


 > tmp
   id author
1  1      A
2  1      B
3  1      C
4  2      D
5  2      E
6  3      F

What is the best (most efficient/vectorized/avoiding loops) approach to 
obtain the following data frame?

id 	author
1	"A, B, C"
2 	"D, E"
3	"F"


Thanks for your help,

Bernd





 > version
                _
platform       i386-pc-mingw32
arch           i386
os             mingw32
system         i386, mingw32
status         Patched
major          2
minor          8.1
year           2008
month          12
day            22
svn rev        47296
language       R
version.string R version 2.8.1 Patched (2008-12-22 r47296)
#
Hi

r-help-bounces at r-project.org napsal dne 29.01.2009 07:52:37:
Not sure if it is most efficient but

 aggregate(tmp$author, list(tmp$id), function(x) paste(x, collapse=","))

can do the trick

Regards
Petr
http://www.R-project.org/posting-guide.html
#
I think you are looking for

split(author, id)


Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of "The R Inferno" and "A Guide for the Unwilling S User")
Weiss, Bernd wrote: