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

David Winsemius dwinsemius at comcast.net
Thu Feb 4 16:52:52 CET 2010

On Feb 4, 2010, at 9:56 AM, J. R. M. Hosking wrote:

> 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.

Are you sure?  (Am I sure?)  Thomas Lumley has corrected my  
misinterpretations on this point (and I apologize to him for the fact  
that he has had to do it more than once.)


I am guessing that the OP was using either sampling weights or  
replication weights (he did not say), so lm( , weights) might not be  
appropriate tool.


David Winsemius, MD
Heritage Laboratories
West Hartford, CT

More information about the R-help mailing list