Hi, My name is MArcus Vinicius and i`m learning to use the R and have a problem. 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? I tried to use the matrix command but it`s only stored the last value of the variable Can i help me? _________________________________________________________________ MSN Messenger: converse com os seus amigos online. http://messenger.msn.com.br
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. -- Jonathan Baron, Professor of Psychology, University of Pennsylvania R page: http://finzi.psych.upenn.edu/
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
ripley at stats.ox.ac.uk writes:> 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.And also note that many of these cases are more succinctly expressed using implicit loops, like Squares <- sapply(1:10,function(x)x^2) or even Squares <- sapply(1:10,"^",y=2) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /''_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907