[R] Suggestions for help & weighted.mean

Duncan Murdoch murdoch at stats.uwo.ca
Fri Aug 11 13:49:11 CEST 2006

Yan Wong wrote:
> Hi, just a quick question:
> Should weighted.mean be able to cope with the specific case where one  
> weight is Inf? I came across this when trying to implement a simple  
> weighted moving average algorithm for surface smoothing: these  
> algorithms often result in a single infinite weight when predicting  
> the surface value at known data points.
> e.g.
>  > weighted.mean(c(77,88,99), c(Inf, 1, 2)) #should this return 77?
> [1] NaN
It makes sense in this case, but in the case where there is more than 
one infinite weight, the result has to be NaN. 

Right now the bit of weighted.mean that does the calculations is

 sum(x * w)/sum(w)

and this would be a lot more complicated if it were to handle this very 
special case.

On the other hand, if you know that in your situation there is at most 
one infinite weight, then you could use

if (any(inf <- is.infinite(w))) x[inf]
else weighted.mean(x, w)

in your own code.

Duncan Murdoch

> Cheers
> Yan
> p.s. a while ago I suggested using '??xxx' as a shortcut for  
> help.search("xxx"), much like '?xxx' is a shortcut for help("xxx"). I  
> was just wondering if anyone had any more thoughts on the matter?
Suggestions like this (and probably the one above) belong more in the 
R-devel list than here.  I think your ?? suggestion is reasonable; why 
don't you write up the necessary patch to implement it, and see if it's 
feasible?   Include that in your post to R-devel, and it will be easier 
for others to see the pitfalls (if there are any).

Duncan Murdoch

More information about the R-help mailing list