0

I'm running a cross correlation analysis and I'm trying to loop the analysis through pairs of columns in a dataframe. I've created a list to populate the resultant output but it doesn't populate correctly. The print function works though.

I've done some research but still have not found the solutions to this problem. Please help.

library(tseries)

blahthresholdtest <- matrix(rnorm(100, 50, 1), nrow = 20, ncol = 4)

thislist <- list()

test <- for(i in seq(1, ncol(blahthresholdtest), 2)){
  for(k in seq(1, ncol(blahthresholdtest), 1)) {
  vector <- numeric(ncol(blahthresholdtest))
  ccftime <- ccf(blahthresholdtest[, i], blahthresholdtest[, i+1],
      type="correlation", na.action=na.omit, plot=FALSE)
  crosscorr <- cbind(ccftime$acf, ccftime$lag)
  crosscorr <- as.data.frame(crosscorr)
  colnames(crosscorr) <- c("CCF", "lag")
  vector[k+1] <- with(crosscorr, lag[CCF == max(CCF)])
  thislist[[k]] <- vector
    }
  print(vector[k])
  print(vector[k + 1])
}

thislist
do.call(rbind, thislist)
Ekatef
  • 1,061
  • 1
  • 9
  • 12
user208959
  • 21
  • 3
  • `?rbind` is used to combine matrices and data frames by rows. It cannot be used to append elements to list. For tips on list appending, see here: https://stackoverflow.com/questions/26508519/how-to-add-elements-to-a-list-in-r-loop and here: https://stackoverflow.com/questions/2436688/append-an-object-to-a-list-in-r-in-amortized-constant-time-o1 – Otto Kässi Oct 18 '18 at 12:28
  • Possible duplicate of [How to add elements to a list in R (loop)](https://stackoverflow.com/questions/26508519/how-to-add-elements-to-a-list-in-r-loop) – Otto Kässi Oct 18 '18 at 14:03

0 Answers0