[R] Finding pairs with least magnitude difference from mean
Hans W Borchers
hwborchers at googlemail.com
Mon Feb 28 20:17:02 CET 2011
<rex.dwyer <at> syngenta.com> writes:
> James,
> It seems the 2*mean(x) term is irrelevant if you are seeking to
> minimize sd. Then you want to sort the distances from smallest to
> largest. Then it seems clear that your five values will be adjacent in
> the list, since if you have a set of five adjacent values, exchanging
> any of them for one further away in the list will increase the sd. The
> only problem I see with this is that you can't use a number more than
> once. In any case, you need to compute the best five pairs beginning
> at position i in the sorted list, for 1<=i<=choose(n,2), then take the
> max over all i.
> There no R in my answer such as you'd notice, but I hope it helps just
> the same.
> Rex
You probably mean something like the following:
x <- rnorm(10)
y <- outer(x, x, "+") - (2 * mean(x))
o <- order(x)
sd(c(y[o[1],o[10]], y[o[2],o[9]], y[o[3],o[8]], y[o[4],o[7]], y[o[5],o[6]]))
This seems reasonable, though you would have to supply a more stringent
argument. I did two tests and it works alright.
--Hans Werner
More information about the R-help
mailing list