[R] tapply and functions with more than one objects

David Winsemius dwinsemius at comcast.net
Wed Jan 23 00:32:36 CET 2013


On Jan 22, 2013, at 2:24 PM, Dominic Roye wrote:

> Hello,
> 
> How i can use a costum function in tapply which has more than one variable?
> 
> I mean sum(x) only needs one object but what when i have a function
> function(x,y) with more, how i indicate where are the other variables
> to use?7

You can use:

lapply(split( multi_col_object, category_vec) , function(x,y){sum(x,y)}  ) 

aggregate(dat, category, FUN=sum)

Or:

do.call(rbind, by( multi_col_object, category_vec, function(x,y){ ....} )

Sometimes `Reduce` is more compact. Other times `mapply` is needed.
-- 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list