Hello all. I have 14 variables, named D2_1, D2_2,...,D2_14 (inherited from a data set). I would like to create a matrix of correlations, although I would be content in just learning how to create a proper do loop. I tried something like this: (for i in 1:14){cor(D2_[i],D2_[i], use "complete.obs")} This is wrong, of course, but I don't know how to "tell" R to run through D2_1, D2_2,...,D2_14 giving me the correlations for each pair. Any help or hints would be appreciated. Sincerely, Robert O'Brien
Hi Robert, There are so many way to do what you want to do. A good staring point would be http://www.nabble.com/Re%3A-applying-cor.test-to-a-%28m%2C-n%29-matrix---SUMMARY-to17150239.html#a17150239 HTH, Jorge On Tue, May 20, 2008 at 6:41 PM, Robert O'Brien < dinklemus_littlelog@yahoo.com> wrote:> Hello all. I have 14 variables, named D2_1, > D2_2,...,D2_14 (inherited from a data set). I would > like to create a matrix of correlations, although I > would be content in just learning how to create a > proper do loop. I tried something like this: > > > (for i in 1:14){cor(D2_[i],D2_[i], use > "complete.obs")} > > This is wrong, of course, but I don't know how to > "tell" R to run through D2_1, D2_2,...,D2_14 giving me > the correlations for each pair. > > Any help or hints would be appreciated. > > Sincerely, > > Robert O'Brien > > ______________________________________________ > 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.[[alternative HTML version deleted]]
Hi, I tried the following one: z <- outer(1:14,1:14,function(i,j) eval(parse(text=paste("cor(D2_",i,",D2_",j,")",sep="")))) but this doen not work (why?). Two alternatives which work are either x <- matrix(0,length(D2_1,14) for (i in 1:14) x[,i] <- eval(parse(text=paste("D2_",i,sep=""))) z <- cor(x) or z <- diag(14) for (i in 1:14) for (j in 1:14) z[i,j] <- eval(parse(text=paste("cor(D2_",i,",D2_",j,")",sep=""))) --- Robert O'Brien <dinklemus_littlelog at yahoo.com> wrote:> Hello all. I have 14 variables, named D2_1, > D2_2,...,D2_14 (inherited from a data set). I would > like to create a matrix of correlations, although I > would be content in just learning how to create a > proper do loop. I tried something like this: > > > (for i in 1:14){cor(D2_[i],D2_[i], use > "complete.obs")} > > This is wrong, of course, but I don't know how to > "tell" R to run through D2_1, D2_2,...,D2_14 giving > me > the correlations for each pair. > > Any help or hints would be appreciated. > > Sincerely, > > Robert O'Brien > > ______________________________________________ > 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. >