[R] multivariate version of aggregate

Jannis bt_jannis at yahoo.de
Fri Jun 28 12:38:18 CEST 2013


Thanks a lot to everybody who responded! My solution now looks similar 
to Ruis and Davids suggestions.


Jannis


On 28.06.2013 11:00, Rui Barradas wrote:
> Hello,
>
> You can solve your problem using only base R, with no need for an 
> external package. The two instrucitons below are two ways of doing the 
> same.
>
>
>
> sapply(split(testframe, indices), function(x) cor(x[, 1], x[, 2]))
>
> as.vector(by(testframe, indices, function(x) cor(x[, 1], x[, 2])))
>
>
>
> Hope this helps,
>
> Rui Barradas
>
> Em 28-06-2013 09:31, Jannis escreveu:
>> 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