[R] Using the R commands

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Sun Mar 30 09:25:34 CEST 2003


On Sat, 29 Mar 2003, Jonathan Baron wrote:

> On 03/30/03 03:25, Marcus V. Stecklow wrote:
> 
> >I`m doing a program and it`s have a loop using the for command. inside the 
> >loop i have a variable and in each step of loop she have a new value.
> >
> >The question is: How can i store the values and make a table?
> 
> For example:
> Squares <- rep(NA,10) # make an empty vector with 10 places
> for (i in 1:10) Squares[i] <- i^2 # fill it with squares
> Squares # print it
> 
> Lots of other ways to do this.  For example, you can use
> Squares <- {}
> for the first step.

The best idea is start out with a vector of the correct length and the 
correct type (rep(NA, 10) is logical), e.g.

Squares <- numeric(10)

This helps for long vectors by reducing the number of copies made.
Squares <- {} is equivalent to setting the result to NULL, just more
obscure.

-- 
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