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. of all 20 values, so I end up with 19 means and stnd. deviations. How would I construct such a loop? Thanks. -- View this message in context: http://www.nabble.com/Loop-with-variable-index-tp15190661p15190661.html Sent from the R help mailing list archive at Nabble.com.
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 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. of all > 20 values, so I end up with 19 means and stnd. deviations.Why (on earth) would you want to do this?> How would I construct such a loop?Let the vector be ``x''. mns <- list() sds <- list() for(i in 2:20) { mns[[i-1]] <- mean(x[1:i]) sds[[i-1]] <- sd(x[1:i]) } mns <- unlist(mns) sds <- unlist(sds) will do what you want. cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
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 <cvandy26 at gmail.com> 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 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. of all > 20 values, so I end up with 19 means and stnd. deviations. > How would I construct such a loop? > Thanks. > -- > View this message in context: http://www.nabble.com/Loop-with-variable-index-tp15190661p15190661.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
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 necessary): +61 7 3826 7304 Mobile: +61 4 8819 4402 Home Phone: +61 7 3286 7700 mailto:Bill.Venables at csiro.au http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of cvandy Sent: Thursday, 31 January 2008 5:59 AM To: r-help at 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. of the first 3 values, etc. until the last time through the loop I want to find the mean and stnd. dev. of all 20 values, so I end up with 19 means and stnd. deviations. How would I construct such a loop? Thanks. -- View this message in context: http://www.nabble.com/Loop-with-variable-index-tp15190661p15190661.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
<Bill.Venables <at> 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 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. of > all 20 values, so I end up with 19 means and stnd. deviations. > How would I construct such a loop? > Thanks.Just for an alternative to some of the sapply solutions, the means can also be obtained with (cumsum(y)/(1:length(y)))[-1] ken