Hello, > > Mar 05, 2012; 8:53pm — by Ben quant Ben quant > Hello, > > Does anyone know of a way I can speed this up? >
Maybe, let's see. > > ################################# change anything below. > # Yes. # First, start by using dates, not characters fdate <- function(x, format="%Y%m%d"){ DF <- data.frame(x) for(i in colnames(DF)){ DF[, i] <- as.Date(DF[, i], format=format) class(DF[, i]) <- "Date" } DF } rd1 <- fdate(rd1) # This is yours, use it. dt1 <- seq(from =as.Date(z.dates[1]), to = as.Date("2009-03-25"), by = "day") # Set up the result, no time expensive 'cbind' inside a loop fin1 <- data.frame(matrix(NA, nrow=length(dt1), ncol=ncol(ua) + 1)) fin1[, 1] <- dt1 nr <- nrow(rd1) # And vectorize for(tkr in 1:ncol(ua)){ x <- c(rd1[, tkr], as.Date("9999-12-31")) inxlist <- lapply(1:nr, function(i) which(x[i] <= dt1 & dt1 < x[i + 1])) sapply(1:length(inxlist), function(i) if(length(ix[[i]])) fin1[ix[[i]], tkr + 1] <<- ua[i, tkr]) } colnames(fin1) <- c("daily_dates", colnames(ua)) # Check results str(fin) str(fin1) head(fin) head(fin1) tail(fin) tail(fin1) Note that 'fin' has facotrs, 'fin1' numerics. I haven't timed it but I believe it should be faster. Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/index-instead-of-loop-tp4447672p4448567.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@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.