Re: [R] Suggestions for help & weighted.mean

From: Duncan Murdoch <>
Date: Fri 11 Aug 2006 - 21:49:11 EST

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
>"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 mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Fri Aug 11 23:00:24 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 12 Aug 2006 - 06:20:14 EST.

Mailing list information is available at Please read the posting guide before posting to the list.