[R] data frames; maybe aggregate?

Phil Spector spector at stat.berkeley.edu
Sat Feb 13 00:58:41 CET 2010


Jonathan -
    split will do what you want:

>  df <- data.frame('id'=c('a','b','a','c','b','a'),'value'=c(6,4,2,9,1,7))
> split(df$value,df$id)
$a
[1] 6 2 7

$b
[1] 4 1

$c
[1] 9

Thanks for providing a reproducible example!

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu



On Fri, 12 Feb 2010, Jonathan Reichel wrote:

> Hi all,
>    Another one:
>
> Given a data frame with two columns, where the first column (let's
> call it 'id') has redundant entries, I'd like to get, for each unique
> value in 'id', a list or vector of all the values from the second
> column that corresponded to that value.
>
> Example:
>
>> df <- data.frame('id'=c('a','b','a','c','b','a'),'value'=c(6,4,2,9,1,7))
>> df
>  id value
> 1  a     6
> 2  b     4
> 3  a     6
> 4  c     9
> 5  b     1
> 6  a     7
>
> I'd like some kind of output resembling something along these lines:
>
> a: 6, 6, 7
> b: 4, 1
> c: 9
>
> Any ideas would be appreciated!
>
> Best,
> Jonathan
>
> ______________________________________________
> 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