Skip to content
Prev 58898 / 398502 Next

(no subject)

On 11-Nov-04 Wei Yang wrote:
Let X = (x1, x2, ... , xn} be your list of numbers.

It seems that what you are looking for is the mean of the set Y:

  Y = {y1, y2, ... , ym} such that, for each yj,

    yj is in X and sum[over i]( (xi - yj)^2 ) < const

Is this right?

If so, you can do it straightforwardly with a loop, like:

  x<-rnorm(100)
  const <-150
  for( i in (1:100) ) {if(sum(x-x[i])^2<const) Y[i]<-x[i]}
  Y[!is.na(Y)]
  [1]  0.17096364 -0.32720155  0.19542299  0.13363724
  [5] -0.19961480 -0.24486536 -0.31485802 -0.33369635
  [9]  0.09981291  0.04263151  0.11127977  0.12144595
 [13] -0.27767009 -0.01242218  0.06244776  0.11646301

  mean(Y[!is.na(Y)])
  [1] -0.04101397

but I'm sure somebody out there will come up with a much
more elegant solution!

Best wishes,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861  [NB: New number!]
Date: 11-Nov-04                                       Time: 20:56:08
------------------------------ XFMail ------------------------------