[R] multivariate version of aggregate

arun smartpink111 at yahoo.com
Fri Jun 28 14:15:22 CEST 2013


Hi,
 set.seed(45)
 testframe <- data.frame(a=rnorm(100), b = rnorm(100))
 indices      <- rep(c(1,2), each = 50)
library(plyr)

ddply(testframe,.(indices),summarize, Cor1=cor(a,b))

#  indices         Cor1
#1       1  0.002770524
#2       2 -0.101738888


A.K.


----- Original Message -----
From: Jannis <bt_jannis at yahoo.de>
To: Greg Snow <538280 at gmail.com>
Cc: r-help <r-help at r-project.org>
Sent: Friday, June 28, 2013 4:31 AM
Subject: Re: [R] multivariate version of aggregate

Yes, I had a look at that function. From the documentation, however, it 
did not get clear to me how to split the dataframe into subsets of rows 
based on an index argument. Like:


testframe <- data.frame(a=rnorm(100), b = rnorm(100))
indices      <- rep(c(1,2), each = 50)


results <- ddply(.data = testframe, INDICES= indices, .fun = function(x) 
corr(x[,1], x[,2]))

Where the last command would yield the correlations between column 1 and 
2 of the first 50 and of the last 50 values.

Any ideas?

Jannis

On 27.06.2013 21:43, Greg Snow wrote:
> Look at the plyr package, probably the ddply function in that package.  You
> can write your own function to do whatever you want on the pieces of the
> split apart object.  Correlation between a specified pair of columns would
> be simple.
>
>
> On Thu, Jun 27, 2013 at 11:26 AM, Jannis <bt_jannis at yahoo.de> wrote:
>
>> Dear List members,
>>
>>
>> i am seeking a multivariate version of aggregate. I want to compute, fro
>> example the correlation between subsets of two vectors. In aggregate, i can
>> only supply one vector with indices for subsets. Is  there ready function
>> for this or do i need to program my own?
>>
>>
>> Cheers
>> Jannis
>>
>> ______________________________**________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/**
>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>

______________________________________________
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