Skip to content

matrix indexing in 'for' loop?

2 messages · govindas at msu.edu, Tom Gottfried

#
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