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

Steve Jaffe sjaffe at riskspan.com
Wed Feb 3 17:21:41 CET 2010


Yes, this is clearly the key to working with subsets. Thanks

-----Original Message-----
From: Petr PIKAL [mailto:petr.pikal at precheza.cz] 
Sent: Wednesday, February 03, 2010 4:16 AM
To: Steve Jaffe
Cc: r-help at r-project.org
Subject: Re: [R] tapply for function taking of >1 argument?

Hi

r-help-bounces at r-project.org napsal dne 02.02.2010 22:16:06:

> 
> 'fraid not :-((
> 
> tapply( data, groups, weighted.mean, weights) 

tapply(seq(along=lll), rrr, function(i, x, w) weighted.mean(x[i], w[i]), 
       x=lll, w=ttt) 
If you want to subset more than one thing, subset the index vector. 
The above help I obtained from Prof.Ripley several years ago so (untested)

tapply( seq(along=data), groups, function (i, x, w) weighted.mean(x[i], 
w[i]), x=data, w=weights)

I believe it shall still work.

Regards
Petr



> 
> won't work because the *entire* weights vector is passed as the 2nd arg 
to
> weighted.means. But weighted.mean needs 'weights' to be split in the 
same
> way as 'data' -- the first and 2nd args need to correspond. 
> 
> 
> Jorge Ivan Velez wrote:
> > 
> > Hi sjaffem,
> > 
> > You were almost there:
> > 
> > tapply( yourdata, groups, weighted.mean, weights)
> > 
> > See ?tapply for more information.
> > 
> > HTH,
> > Jorge
> > 
> > 
> > 
> 
> -- 
> View this message in context: 
http://n4.nabble.com/tapply-for-function-taking-
> of-1-argument-tp1460392p1460419.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list