[R] Noob question - Identity argument within aggregate function?

David Winsemius dwinsemius at comcast.net
Sat Mar 3 15:24:36 CET 2012

Context added:

On Mar 2, 2012, at 3:51 PM, knavero wrote:

>> aggregate(z, identity, mean)
> 1   2   3   4   5
> 1.0 3.0 5.0 6.0 7.5
>> aggregate(z, mean)
> Error: length(time(x)) == length(by[[1]]) is not TRUE

As generally happens when you call a function and fail to provide  
enough arguments to fill up its formals list.

> Can someone help me understand the error above and why "identity" is
> necessary to satisfy the error

Well on my machine it throws an error, probably because you failed to  
provide the requested code to create the objects you were working on.  
Is 'z' so sort of special classed object for which there is an  
aggregate method? Is 'identity' a list as expected by  
aggregate.default or aggregate.data.frame? It would be an unfortunate  
choice of an object name, since there is a function with that nam.

---------end context -----------------

On Mar 2, 2012, at 8:30 PM, knavero more recently wrote:

> I've also searched "?identity" in the R shell and it doesn't seem to  
> be the
> definition I'm looking for for this particular usage of "identity"  
> as an
> argument in the aggregate function. I simply would appreciate a  
> conceptual
> explanation of what it does here and how it relates to the error.

I see no relation of the function `identity` to the error. It was  
simply an error caused by supplying 'mean` as the second arument to  
aggregate.zoo. The second argument in all of the aggregate functions  
is a grouping argument. In the case of aggregate.zoo that pattern has  
been generalized to allow a function to be applied to the index, but  
when aggregate.zoo attempted to apply `mean` to the index vector it  
did not return a factor object of the same length as the time vector,  
so there was no sensible grouping that could be formed formed.

As the help page (to which Dr Dalgaard referred you) says: `identity`  
got applied to the zoo-index. So its use in the first instance  
succeeded in creating a vector of the proper length so that when  
coerced to a factor provided a grouping. You could have supplied any  
function that created a vector length 8.

 > index(z) < 3

 > aggregate(z, function(x){ x < 3}, mean)
   6.5   2.5

Now. ... Will you PLEASE read the Posting Guide and stop sending  
emails to Rhelp with no context. The posting behavior of new Nabble  
users is a constant annoyance to the list members who view this list  
with their mail clients. This is NOT Nabble and most of us do not want  
to use a web interface to read Rhelp.

David Winsemius, MD
West Hartford, CT

More information about the R-help mailing list