R Sapply Problem

[This article was first published on Quantitative Finance Collector, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

To leave a comment for the author, please follow the link and comment on their blog: Quantitative Finance Collector.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)