[R] How to avoid a for-loop?
Oliver Faulhaber
oliverfaulhaber at gmx.de
Mon Mar 12 20:16:28 CET 2007
Hi all,
as I am trying to move slowly from just "working" to "good" code, I'd
like to ask if there's a smarter way than using a for-loop in tasks like
the example below.
I need to obtain the extrema of the cumulated sum of a detrended time
series. The following code is currently used, please have a look at the
comments for my questions and remarks:
system.time({
X <- rnorm(10000)
X.length <- length(X)
X.cum.sum <- cumsum(X)
X.cum.mean <- cummean(X)
# initializing the "output" vectors
X.min.detrended <- rep(NA,X.length)
X.max.detrended <- rep(NA,X.length)
for (i in 1:X.length) {
# Detrending of the time series from index 1 to i
# I think that's the time consuming part, are there any
# suggestions how to do this faster?
X.cum.sum.detrended <- X.cum.sum[1:i]-seq(1:i)*X.cum.mean[i]
# Calculating the min and max. Would a "range" be smarter here?
X.min.detrended[i] <- min(X.cum.sum.detrended)
X.max.detrended[i] <- max(X.cum.sum.detrended)
# As the programs takes rather long to complete I would like to
# get information about the progress. Any better way to do this
# than a cat(paste(i,"...",""))?
}
})
As you can see this takes rather long even for this relative small sample:
[1] 41.11 0.97 45.14 NA NA
I considered using "sapply", but expected problems with memory size as a
lot more values have to be kept in memory (the typical length of X is
10^6 - 10^7).
Thanks again for your patience with newbies like me :)
Regards
Oliver
More information about the R-help
mailing list