Weiss, Bernd
2009-Jan-29 06:52 UTC
[R] Question about collapse/aggregate and avoidance of loops
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)
Petr PIKAL
2009-Jan-29 07:38 UTC
[R] Odp: Question about collapse/aggregate and avoidance of loops
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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Patrick Burns
2009-Jan-29 08:46 UTC
[R] Question about collapse/aggregate and avoidance of loops
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:> 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) > > ______________________________________________ > 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. > >