[R-sig-Geo] matrix indexing in 'for' loop?

Tom Gottfried tom.gottfried at wzw.tum.de
Wed Dec 22 13:28:51 CET 2010


Hi Maha,

note that factors in your example is a character vector. So y is too. And then you try to retrieve 
the values in your matrix from y...
You could put your matrices in a list and then use list-indexing in your outer loop:

ts.m.dmi <- matrix(c(1:20), 4, 5)
ts.m.soi <- matrix(c(21:40), 4, 5)
ts.m.pe <- matrix(c(21:40), 4, 5)

factors <- list(ts.m.dmi, ts.m.soi)
for (j in 0:1){
	y <- factors[[j+1]]

	for (i in 1:5){

	cor.pe.y <- cor(ts.m.pe[,2], y[,i])
	ct.tst <- cor.test(ts.m.pe[,2], y[,i])
	}
}

Tom
-- 
Technische Universität München
Department für Pflanzenwissenschaften
Lehrstuhl für Grünlandlehre
Alte Akademie 12
85350 Freising / Germany
Phone: ++49 (0)8161 715324
Fax:   ++49 (0)8161 713243
email: tom.gottfried at wzw.tum.de
http://www.wzw.tum.de/gruenland



More information about the R-sig-Geo mailing list