[R] tapply for function taking of >1 argument?

J. R. M. Hosking JRMH001 at gmail.com
Thu Feb 4 15:56:57 CET 2010

sjaffe wrote:
> I'm sure I can put this together from the various 'apply's and split, but I
> wonder if anyone has a quick incantation:
> E.g. I can do tapply( data, groups, mean)
> but how can I do something like:  tapply( list(data,weights), groups,
> weighted.mean ) ?
> (or: mapply is to sapply as ? is to tapply )
> Thanks for your help.

   coef(lm(data ~ -1 + as.factor(groups), weights=weights))

Not the fastest, but IMO more comprehensible than the constructions
involving anonymous functions.

J. R. M. Hosking

More information about the R-help mailing list