[R] avoiding loops

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jun 25 10:53:04 CEST 2004


On Fri, 25 Jun 2004, Anne Piotet wrote:

> I'd like to compute the values
> x(i)-x(n-i-1) for i=1 n/2 or similar   (as way of testing distribution symmetry hypothesis). It is very easy to do that with a loop but can that be avoided? 

Is that really what you want (not x[n-i+1])?  x - rev(x) gives you the
latter for all i.  Or try

ind <- seq(1, floor(n/2))  (you don't need the floor, but it is clearer)
x[ind] - x[n - ind + 1]

(or -1 if you prefer).

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list