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

Re: [R] Loop with variable index

2008-01-30 Thread Bill.Venables
Sent: Thursday, 31 January 2008 5:59 AM To: r-help@r-project.org Subject: [R] Loop with variable index 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 two values; the second time through the loop I want to find the mean and stnd. dev.

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

[R] Loop with variable index

2008-01-30 Thread cvandy
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 two values; the second time through the loop I want to find the mean and stnd. dev. of the first 3 values, etc. until the last time through the loop I want to find the mean and stnd. dev.