Alexy Khrabrov wrote: > I wonder what's the right way in R to do the following -- placing > objects of the same kind together in subarrays of varying length. > Here's what I mean: > > > word <- c("a","b","c","d","e","f","g","h","i","j") > > kind <- c(1,1,1,2,3,4,5,5,7,7) > > d <- data.frame(word,kind) > > d > word kind > 1 a 1 > 2 b 1 > 3 c 1 > 4 d 2 > 5 e 3 > 6 f 4 > 7 g 5 > 8 h 5 > 9 i 7 > 10 j 7 > > Now from this data frame, I want to assemble words of the same kind > into lists. The result should look like (not R syntax): > > 1 => [a,b,c] > 2 => [d] > 3 => [e] > 4 => [f] > 5 => [g,h] > 7 => [i,j] > > What is the most appropriate data structure in R for this result and > growing these sublists most effectively with append?
It would make sense to use a list as the data structure, and here is how you might do it: with(d, split(word, kind)) # OR with(d, split(as.character(word), kind)) ?split > Cheers, > Alexy > > ______________________________________________ > R-help@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. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 ______________________________________________ R-help@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.