[R] Odp: Question about collapse/aggregate and avoidance of loops
Petr PIKAL
petr.pikal at precheza.cz
Thu Jan 29 08:38:57 CET 2009
Hi
r-help-bounces at r-project.org napsal dne 29.01.2009 07:52:37:
> 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"
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
>
>
> 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)
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list