[R] ragged array with append

Alexy Khrabrov deliverable at gmail.com
Sat Nov 24 11:59:37 CET 2007


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?

Cheers,
Alexy



More information about the R-help mailing list