From arnima at hafro.is  Thu Oct 29 19:00:16 2009
From: arnima at hafro.is (arnima at hafro.is)
Date: Thu, 29 Oct 2009 19:00:16 +0100 (CET)
Subject: [Rd] weighted.mean uses zero when na.rm=TRUE (PR#14032)
Message-ID: <20091029180016.B4EEE282EFFB@mail.pubhealth.ku.dk>

The weighted.mean() function replaces NA values with 0.0 when the user 
specifies na.rm=TRUE:

   x <- c(101, 102, NA)
   mean(x, na.rm=TRUE)                         # 101.5, correct
   weighted.mean(x, na.rm=TRUE)                # 67.66667, wrong
   weighted.mean(x, w=c(1,1,1), na.rm=TRUE)    # 67.66667, wrong
   weighted.mean(x, w=c(1,1,1)/3, na.rm=TRUE)  # 67.66667, wrong

The weights are normalized w<-w/sum(w) before removing the NA values, 
effectively replacing x[is.na(x)]<-0. This bug was introduced between 
versions 2.9.2 and 2.10.0.

Thanks,

Arni



[Rd] weighted.mean uses zero when na.rm=TRUE (PR#14032)
arnima at hafro.is