I have a list as follows:
id | value
----------
4 600
4 899
7 19
13 4930
13 300
: :
There are multiple ID repeats, and each one has a unique value. I want to turn this into something as follows:
id | list
----------
4 c(600, 899)
7 c(19)
13 c(4930, 300)
: :
Is there a vectorized method of accomplishing this?
EDIT: Extending the first question, is there a simple way to do the same thing for a generic MxN matrix? I.e., turning this:
id | value1 value2
-------------------
4 600 a
4 899 b
7 19 d
13 4930 e
13 300 a
: : :
into this:
id | list
----------
4 list(c(600, 899),c('a','b'))
7 list(c(19),c('b'))
13 list(c(4930, 300),c('e','a'))
: :
Thanks!
You could also use
tapplyif you want to stick with base functions:Edit:
For your edited problem, I would go with
splitandlapply: