Skip to content
Prev 74886 / 398502 Next

using weighted.mean with tapply()

roger bos wrote:
Hi -- I asked pretty much this same question some years ago:

http://www.r-project.org/nocvs/mail/r-help/1999/2160.html

The answer turns out to be that you should pass the index to tapply
rather than the data. In your case this would, I think, translate to

decile <- tapply(seq(along=mat$trlm, mat$Rank,
function(i, x=mat$trlm[i], w=mat$mcap[i])
weighted.mean(x[i], w[i]))

hope this helps.

regards,

Markus