Reputation: 39
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]
}
}
}
Upvotes: 1
Views: 77
Reputation: 79318
ES
[1] -0.30114431 -0.97900009 1.04164231 0.64237371 0.44924653
[6] 0.24134990 -0.20286873 0.49661435 0.30273617 0.05852534
n=which(ES<0)
key_recorded=key #Create a duplicate of key and store it in key_recorded
key_recorded[,n][key[,n]==2]=0# Store 0 in key_recorded wherever key is 2
key_recorded[,n][key[,n]==0]=2# Store 2 in key_recorded wherever key is 0
head(key)
[,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,] 1 1 1 1 1 1 1 1 1 1
[6,] 2 2 2 2 2 2 2 2 2 2
head(key_recorded)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 2 2 0 0 0 0 2 0 0 0
[2,] 0 0 2 2 2 2 0 2 2 2
[3,] 2 2 0 0 0 0 2 0 0 0
[4,] 0 0 2 2 2 2 0 2 2 2
[5,] 1 1 1 1 1 1 1 1 1 1
[6,] 0 0 2 2 2 2 0 2 2 2
Now as you can see the required columns are flipped when ES<0 but when ES>=0, the columns are maintained
Upvotes: 2
Reputation: 1573
I think Onyambu's answer doesn't take in account if ES have negative value:
key2 <- key
for(i in 1:length(ES)){
if(ES[i] < 0){
key2[,i] <- ifelse(key2[,i] == 2, 0, ifelse(key2[,i] == 0, 2, key2[,i]))
}
}
head(key)
head(key2)
Upvotes: 2