[R] tapply for function taking of >1 argument?
sjaffe at riskspan.com
Wed Feb 3 17:21:41 CET 2010
Yes, this is clearly the key to working with subsets. Thanks
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?
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]),
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.
> won't work because the *entire* weights vector is passed as the 2nd arg
> weighted.means. But weighted.mean needs 'weights' to be split in the
> 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:
> Sent from the R help mailing list archive at Nabble.com.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help