Hi, Here is my problem. Say I have two matrices, Matrix A: a b c 4 5 2 3 2 1 4 5 6 Matrix B: d e f g h 3 4 5 2 1 2 3 7 8 6 8 5 1 3 4 I would like to correlate vector a of matrix A with with vectors d,e,f,g,h of matrix "B" and save the p-values (or some type of statistic) in a seperate result vector, say z. Then, I would like to repeat this process for vector b and vector c of matrix A. In the present example, vector z would end up containg 15 elements. I tried a few double loops, but was not successful in saving the results in a vector. I was only able to print them with the "print" or "cat" command, but could not turn the print or cat results into an accessible object. Help would be appreciated. Thank you, Frank. -- or over the web
you could consider something like this: A <- matrix(rnorm(100 * 3), 100, 3); colnames(A) <- letters[1:3] B <- matrix(rnorm(100 * 5), 100, 5); colnames(B) <- letters[4:8] #### z <- matrix( 0, ncol(A), ncol(B), dimnames = list(colnames(A), colnames(B)) ) for(i in 1:ncol(A)){ z[i, ] <- apply(B, 2, function(x, y) cor.test(x, y)$p.value, y = A[, i]) } z I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Frank Johannes" <fjohannes at fastmail.fm> To: <r-help at stat.math.ethz.ch> Sent: Monday, December 05, 2005 4:28 PM Subject: [R] loop problem> Hi, > Here is my problem. Say I have two matrices, > > Matrix A: > > a b c > 4 5 2 > 3 2 1 > 4 5 6 > > Matrix B: > d e f g h > 3 4 5 2 1 > 2 3 7 8 6 > 8 5 1 3 4 > > I would like to correlate vector a of matrix A with with vectors > d,e,f,g,h of > matrix "B" and save the p-values (or some type of statistic) in a > seperate result vector, say z. Then, I would like to repeat this > process > for vector b and vector c of matrix A. In the present example, > vector z > would end up containg 15 elements. > I tried a few double loops, but was not successful in saving the > results > in a vector. I was only able to print them with the "print" or "cat" > command, but could not turn the print or cat results into an > accessible > object. > > Help would be appreciated. > Thank you, > Frank. > > -- > > or over the web > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Hello Frank> Hi, > Here is my problem. Say I have two matrices, > > Matrix A: > > a b c > 4 5 2 > 3 2 1 > 4 5 6 > > Matrix B: > d e f g h > 3 4 5 2 1 > 2 3 7 8 6 > 8 5 1 3 4Please produce a reproducable example, like A <- matrix(rnorm(9),ncol=3) B <- matrix(rnorm(15), ncol=5) Here is just the loop for you: n <- 0 z <- vector() # or z <- 1:(dim(A)[2]+dim(B)[2]) for(i in 1:dim(A)[2]){ for( j in 1:dim(B)[2] ){ n <- n + 1 z[n] <- cor(A[,i], B[,j]) } } I hope this helps, Matthias> > I would like to correlate vector a of matrix A with with > vectors d,e,f,g,h of matrix "B" and save the p-values (or > some type of statistic) in a seperate result vector, say z. > Then, I would like to repeat this process for vector b and > vector c of matrix A. In the present example, vector z would > end up containg 15 elements. > I tried a few double loops, but was not successful in saving > the results in a vector. I was only able to print them with > the "print" or "cat" command, but could not turn the print or > cat results into an accessible object. > > Help would be appreciated. > Thank you, > Frank. > > -- > > or over the web > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read > the posting guide! http://www.R-project.org/posting-guide.html >