Re: [R] Loop with variable index

2008-01-30 Thread Ken Knoblauch
csiro.au> writes: > > y <- sort(rnorm(20)) # say... > > m <- s <- numeric(19) > > for(i in 2:20) { > m[i-1] <- mean(y[1:i]) > s[i-1] <- sd(y[1:i]) > } > -Original Message- > On Behalf Of cvandy > Subject: [R] Loop with variable index > I have a list of 20 values. The fir

Re: [R] Loop with variable index

2008-01-30 Thread Bill.Venables
y <- sort(rnorm(20)) # say... m <- s <- numeric(19) for(i in 2:20) { m[i-1] <- mean(y[1:i]) s[i-1] <- sd(y[1:i]) } Easy peasy, ... Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessa

Re: [R] Loop with variable index

2008-01-30 Thread Benilton Carvalho
a cleaner code would be: sapply(2:20, function(i) c(mean=mean(x[1:i]), sd=sd(x[1:i]))) b On Jan 30, 2008, at 3:16 PM, Henrique Dallazuanna wrote: Try this: x <- rnorm(20) sapply(c("sd", "mean"), function(fun)lapply(lapply(lapply(2:20, seq, from=1), function(.x)x[.x]), fun)) On 30/01/2008,

Re: [R] Loop with variable index

2008-01-30 Thread Henrique Dallazuanna
Try this: x <- rnorm(20) sapply(c("sd", "mean"), function(fun)lapply(lapply(lapply(2:20, seq, from=1), function(.x)x[.x]), fun)) On 30/01/2008, cvandy <[EMAIL PROTECTED]> wrote: > > I have a list of 20 values. The first time through a loop I want to find the > mean and stnd.dev. of the first t

Re: [R] Loop with variable index

2008-01-30 Thread Rolf Turner
On 31/01/2008, at 8:58 AM, cvandy wrote: > > I have a list of 20 values. ***NO***! You have (or should have) a *vector* of 20 values. Vectors and lists are different concepts. Learn and understand the difference, else the world will come to an end. > The first time thr