lundi 29 janvier 2018

Nested For Loops w/ If-Else

I've never done a nested for loop before and my understanding of for loops is only rudimentary anyway. I am trying to get the loop to iterate over rows of a matrix and then elements of a vector to write to an empty matrix. I am also open to non-loop/vectorized solutions, though I would love to know why my loop code is so hopelessly broken.

To set up the input matrices and vector:

condition = c(0,1,2)
condition_risk = c(0,1,1,2,2,2)
condition_health = c(0,0,0,1,1,2)
ES = rnorm(n = 10)

ppt1 = matrix(sample(condition, 10, replace = T), ncol = 10, nrow = 100)
ppt2 = matrix(sample(condition_risk, 10, replace = T), ncol = 10, nrow = 25)
ppt3 = matrix(sample(condition_health, 10, replace = T), ncol = 10, nrow = 25)

key = rbind(ppt1,ppt2,ppt3)
key_recoded = matrix(NA, ncol = 10, nrow = 150)

This creates a vector of effects -1 to 1 called "ES"; a matrix w/ conditions 0,1,2 called "key"; and an empty matrix with the same dimensions called "key_recoded":

head(ES)
-0.31741374 -0.08023316 -0.57528823  0.78028852 -0.20937815  0.12266581

head(key)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    2    2    2    2    2    2    2    2    2     2
[2,]    0    0    0    0    0    0    0    0    0     0
[3,]    2    2    2    2    2    2    2    2    2     2
[4,]    0    0    0    0    0    0    0    0    0     0
[5,]    0    0    0    0    0    0    0    0    0     0
[6,]    2    2    2    2    2    2    2    2    2     2

head(key_recoded)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
[2,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
[3,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
[4,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
[5,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
[6,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA

The goal is to recode the conditions from 0,1,2 to 2,1,0 when ES values are negative.

For example, ES[1] = -.31741374 and key[1,1] = 2, so key_recoded [1,1] should be 0 instead of 2. If key[1,1] had instead been 0, then key_recoded would be 2 instead of 0. Condition values of 1 in "key" are to be ignored.

This is what key_recoded should look like; see that 0 and 2 re flipped from key w/ 0s in the 1st row instead of 2s:

head(key_recoded)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    0    0    0    0    0    0    0    0     0
[2,]    2    2    2    2    2    2    2    2    2     2
[3,]    0    0    0    0    0    0    0    0    0     0
[4,]    2    2    2    2    2    2    2    2    2     2
[5,]    0    0    0    0    0    0    0    0    0     0
[6,]    2    2    2    2    2    2    2    2    2     2

This is my sad code that does nothing to the empty matrix, "key_recoded", but returns no errors:

for (j in nrow(key)){
  for (i in nrow(ES)){
    if(ES[i] < 0 && key[j,i] == 2){
      key_recoded[j,i] = 0
    }
    else{
      key_recoded[j,i] = key[j,i]
    }
    if(ES[i] < 0 && key[j,i] == 0){
      key_recoded[j,i] = 2
    }
    else{
      key_recoded[j,i] = key[j,i]
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire