[R] Writing My Own Function to Use With aggregate

David Winsemius dwinsemius at comcast.net
Thu Sep 2 00:47:39 CEST 2010


On Sep 1, 2010, at 6:29 PM, Adrian Ng wrote:

> Dear R-Users,
>
> I have been using R for about 1 week and recently learned about the  
> Aggregate function, and from reading online it seems like it is  
> comparable to a SQL group by to perform summary functions.  I am  
> looking to do a summary function, such as SUM/MIN, but instead of  
> these basic functions, I would like to calculate the IRR.
>
> Assuming I have a data set DS01:
> FirstName         LastName         NCF      Date
> A                      B                      -100                   
> 1/1/2001
> A                      B                      50                      
> 2/1/2002
> A                      B                      200                    
> 3/1/2003
> A                      C                      -500                   
> 1/1/2001
> A                      C                      50                      
> 2/1/2002
> A                      C                      70                      
> 3/1/2003
> A                      C                      50                      
> 2/1/2004
> A                      C                      70                      
> 3/1/2005
>
> And an IRR function which takes in a cash flow and dates as inputs:  
> IRR(NCF,cfDate) and returns the IRR
>
> I tried the following:
> aggregate(DS01$NCF,by=list(DS01$ FirstName,DS01$  
> LastName),RR,cfDate=DS01$Date)

Since you are not forthcoming with the code for IRR (nor did you even  
spell it correctly in your aggregate call), I will simple suggest that  
you consider using the functions from the plyr package or if you  
wanted to do it the old way you could look up split() and do :

lapply (split(DS01, list(DS01$ FirstName,DS01$ LastName), IRR(NCF,  
cfDate=Date) ).

Plyr method (perhaps):

require(plyr)
ddply(DS01, .( FirstName, LastName), function(df) IRR(NCF,  
cfDate=Date) )

I'm quite sure that the data.table functionality could handle it quite  
easily as well.


>
> and I got the following error:
> Error in aggregate.data.frame(as.data.frame(x), ...) :
>  arguments must have same length
>
> If anyone could shed some light on what may be causing this that  
> would be great.

aggregate() takes a vector while you need to pass a more complex  
structure. I think you passed the entire length of Date.


> More importantly, is this the right way to do this (should I even be  
> using the aggregate function)?
>
> Any help would be greatly appreciated!
> Thanks!
>
>
>
>
>
> Please consider the environment before printing this e-mail.
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list