Hello, I've got the following problem. I have to matrices each containing 200 time series. Now I want to calculate the correlation of the first time series of each of the matrices. I use the following command: cor(mts1[,1],mts2[,1], use="complete.obs", method=c("pearson")) cor(mts1[,2],mts2[,2], use="complete.obs", method=c("pearson")) cor(mts1[,3],mts2[,3], use="complete.obs", method=c("pearson")) and so on.. I would like to repeat this for each of the 200 time series. As it is quite painful to change the command 200 times I wanted to ask if there's a loop function that can cover these series in a fast way? Thanks in advance for your help Best Tom -- View this message in context: http://r.789695.n4.nabble.com/Correlation-Loops-in-time-series-tp4672732.html Sent from the R help mailing list archive at Nabble.com.
Hi, May be this helps: set.seed(25) mt1<- matrix(sample(c(NA,1:40),20*200,replace=TRUE),ncol=200) set.seed(487) mt2<- matrix(sample(c(NA,1:80),20*200,replace=TRUE),ncol=200) res<- sapply(seq_len(ncol(mt1)),function(i) cor(mt1[,i],mt2[,i],use="complete.obs",method="pearson")) A.K. Hello, I've got the following problem. I have to matrices each containing 200 time series. Now I want to calculate the correlation of the first time series of each of the matrices. I use the following command: cor(mts1[,1],mts2[,1], use="complete.obs", method=c("pearson")) cor(mts1[,2],mts2[,2], use="complete.obs", method=c("pearson")) cor(mts1[,3],mts2[,3], use="complete.obs", method=c("pearson")) and so on.. I would like to repeat this for each of the 200 time series. As it is quite painful to change the command 200 times I wanted to ask if there's a loop function that can cover these series in a fast way? Thanks in advance for your help Best Tom
sapply(1:200, function(x) cor(mts1[,x], mts2[,x], use="complete.obs", method=c("pearson"))) ------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of TMiller Sent: Wednesday, July 31, 2013 8:16 AM To: r-help at r-project.org Subject: [R] Correlation Loops in time series Hello, I've got the following problem. I have to matrices each containing 200 time series. Now I want to calculate the correlation of the first time series of each of the matrices. I use the following command: cor(mts1[,1],mts2[,1], use="complete.obs", method=c("pearson")) cor(mts1[,2],mts2[,2], use="complete.obs", method=c("pearson")) cor(mts1[,3],mts2[,3], use="complete.obs", method=c("pearson")) and so on.. I would like to repeat this for each of the 200 time series. As it is quite painful to change the command 200 times I wanted to ask if there's a loop function that can cover these series in a fast way? Thanks in advance for your help Best Tom -- View this message in context: http://r.789695.n4.nabble.com/Correlation-Loops-in-time-series -tp4672732.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at 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.