嵌套for循环w / If-Else

时间:2018-01-29 20:14:55

标签: r for-loop if-statement nested

我以前从未做过嵌套for循环,而且我对for循环的理解无论如何都只是基本的。我试图让循环迭代矩阵的行,然后迭代的元素写入空矩阵。我也对非循环/矢量化解决方案持开放态度,但我很想知道为什么我的循环代码如此无可救药地被打破。

设置输入矩阵和矢量:

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)

这会创建一个效果-1到1的向量,称为“ES”;条件为0,1,2的矩阵称为“密钥”;和一个名为“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

目标是在ES值为负时重新编码从0,1,2到2,1,0的条件。

例如,ES [1] = -.31741374和key [1,1] = 2,因此key_recoded [1,1]应该是0而不是2.如果key [1,1]改为0,然后key_recoded将是2而不是0.“key”中的条件值1将被忽略。

这就是key_recoded的样子;看到0和2从第1行中的键w / 0而不是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

这是我遗憾的代码,它对空矩阵“key_recoded”没有任何作用,但是没有返回错误:

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]
    }
  }
}

2 个答案:

答案 0 :(得分:2)

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

现在您可以看到,当ES <0时,所需的列被翻转,但当ES&gt; = 0时,列被维护

答案 1 :(得分:2)

我认为,如果ES有负值,Onyambu的答案并没有考虑到:

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)