Any expert in R please educates me. I have got a problem about the sapply (or lapply), it made me headache for over two hours.

As “for loop” is very slow in R, we should try best to avoid using it, and to use vectorization instead. sapply is designed for this, for example, instead of:

for (i in 1:10) {

z[i] <- mean(x[1:i])

}

we could use

z <- sapply(1:10, function(i, x) {mean(x[1:i])}, x)

It went well, but what if besides computing z, I need to update another variable, for example, with loop, it is

temp <- 3

for (i in 1:10) {

x[i] <- x[i]-temp

z[i] <- mean(x[1:i])

temp <- x[i]-z[i]

}

in this case, temp is changing every step (it doesn’t have to be a function of z[i]). How to vectorize that and use sapply then? since sapply can’t return two variables z and temp.

~~I tried to define a matrix and store z in the first column and temp in the second column and return the matrix, however, failed.~~

Many thanks.

**PS**, NO, still not correct, working on it…

~~ah, I worked it out, it can be done by passing z itself to sapply, that’s good.~~

the following is a sapply example returning the same result for “for loop” and “sapply”.

sapply.example <- function(nsim = 10){

x <- rnorm(nsim)

y <- list()

z.for <- array(0, nsim)

temp <- 3

for (i in 1:nsim) {

x[i] <- x[i]-temp

z.for[i] <- mean(x[1:i])

temp <- x[i]-z.for[i]

}

y$z.for <- z.for

z.sapply <- array(0,2*nsim)

z.sapply[1] <- 3

z.sapply <- sapply(seq(1,2*nsim,by=2), function(i,x,z.sapply) {

temp <- z.sapply[i]

z.temp <- mean(x[1:((i+1)/2)])

temp <- x[((i+1)/2)]-z.temp

z.sapply[i] <- temp

z.sapply[i+1] <- z.temp

z.sapply[i:(i+1)]

}, x, z.sapply, simplify =TRUE)

y$z.sapply <- z.sapply[seq(2,2*nsim, by=2)]

y

}

Tags – r

**Read the full post at R Sapply Problem**.

*Related*

R-bloggers.com offers

**daily e-mail updates** about

R news and

tutorials on topics such as: visualization (

ggplot2,

Boxplots,

maps,

animation), programming (

RStudio,

Sweave,

LaTeX,

SQL,

Eclipse,

git,

hadoop,

Web Scraping) statistics (

regression,

PCA,

time series,

trading) and more...

If you got this far, why not

__subscribe for updates__ from the site? Choose your flavor:

e-mail,

twitter,

RSS, or

facebook...