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.
roger bos wrote:> 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() >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> >>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 at 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 >-- Markus Jantti Abo Akademi University markus.jantti at iki.fi http://www.iki.fi/~mjantti
On Wed, 2005-08-03 at 17:00 -0400, roger bos wrote:> 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.When you use tapply, only the first argument of the function you supply is split by the index variable. There is no way for tapply to know if further arguments should be split or not, so it doesn't split them. People are regularly caught out by this. If you look carefully at the documentation for split, there is a warning about it. Here is a very dense solution (due to Peter Dalgaard): by(mat, mat$Rank, with, weighted.mean(trt1m, mcap)) Alternatively you can split up the data frame using split, write your own custom wrapper function for doing the weighted mean, and use lapply or sapply: sapply(split(mat,mat$Rank), function(x) {weighted.mean(x$trt1m,x$mcap)}) Martyn ----------------------------------------------------------------------- This message and its attachments are strictly confidential. ...{{dropped}}