[R] using weighted.mean with tapply()

From: roger bos <roger.bos_at_gmail.com>
Date: Thu 04 Aug 2005 - 07:00:40 EST


I am trying to calculate the weighted mean for a of 10 deciles and I get an error:
> decile <- tapply(X=mat$trt1m, INDEX=mat$Rank, FUN=weighted.mean, w=mat$mcap)
Error in FUN(X[[1]], ...) : 'x' and 'w' must have the same length

All three of my inputs have the same length, as shown below, and the weighted.mean calculation works by itself, just not in tapply()

> length(mat$Rank)

[1] 1853
> length(mat$mcap)

[1] 1853
> length(mat$trt1m)

[1] 1853
> mean(mat$trt1m)

[1] -0.04475397
weighted.mean(mat$trt1m, w=mat$mcap)
[1] -0.04819243
> mat$mcap[is.na(mat$mcap)] <- min(mat$mcap, na.rm=TRUE)

I am probably making a simple error in how I pass the optional parameter w. Any help would be greatly appreciated.



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Aug 04 07:08:43 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:40 EST