[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.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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])
}
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]
}
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.
Many thanks.
PS, NO, still not correct, working on it…
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
}
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.