That certainly helps Petr. In terms of the overall question, perhaps it is better to show the code and reexplain what exactly I am trying to do:
1. I have logetf values in a [3300,9] matrix 2. I am trying to find the w value in the function: sum(abs(retT-w*prcomp(ret[(1+i):(252+i),]$rotation[,1]) 3. retT are time T log etf value, prcomp(ret[(1+i):(252+i),]$rotation[,1] is the Principal component value for 252 days prior to time T. 4. I want to utilize the DEoptim() function in a loop from time T (the beginning T value is logetf value on the 253rd day) until the last day, which would correspond with nrow(ret) [or around 3300]. DEoptim will find the w value that minimizes that function. #etf dataset logetf=log(etf) ret=as.matrix(logetf, nrow=dim(logetf)[1], ncol=dim(logetf)[2]) #principal component using eigenvalues library(DEoptim) library(zoo) retT=ret[c(253:nrow(ret)),] retQ=ret[c(1:252),] pr=prcomp(retQ) pr1=pr$rotation[,1] pr1=matrix(pr1,nrow=1, ncol=9) #w1 Values for(i in 252:nrow(ret)){ fn<-function(w) {sum(abs(retT-w*prcomp(ret[(1+i):(252+i),]$rotation[,1])))}} lower<-c(-1000000) upper<- -lower set.seed(1234) record=DEoptim(fn, lower, upper) If there is any more information I can provide to make it more clear, please let me know. My coding skills is really subpar for this project but I need to finish it asap. I REALLY appreciate anyone's help! -- View this message in context: http://r.789695.n4.nabble.com/Help-with-a-repeating-process-tp4634336p4634356.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.