### 如果不是这样的话

1）对于处理等于“ 混合”的行，我想找到NA，然后；

2a）如果NA不属于 pun1 pun2 之下；

eva3 eva4 的整数必须分别转换/复制为 eva_out1 eva_out2 ，< / p>

2b）反之亦然，如果NA在 pun3 pun4 下，则;

eva1 等于 eva_out1 ，其中 eva2 也将其整数转换为 eva_out2

``````df5\$eva_out1 <-  with(df5, ifelse(
(is.na(pun1) | is.na(pun2)) & treatment == "mixed",
df5\$eva_out1 <- eva3,NA ))
``````

``````  df5\$eva_out1 <-  with(df5, ifelse(
(is.na(pun1) | is.na(pun2)) & treatment == "mixed",
df5\$eva_out1 == eva2 & df5\$eva_out2 == eva3, ifelse(
(is.na(pun3) | is.na(pun4)) & treatment == "mixed",
df5\$eva_out1 == eva1 & df5\$eva_out2 == eva2, NA )))
``````

``````if(df5\$treatment == "mixed") {
} if ( is.na(pun1) | is.na(pun2) ) {
eva_out1 <- eva3 & eva_out2 <- eva4
} else if ( is.na(pun3) | is.na(pun4) ) {
eva_out1 <- eva1 & eva_out2 <- eva2
} else {
eva_out1 <- NULL & eva_out2 <- NULL
}
``````

``````.     UniqueSS subject group   part   round  treatment pun1 pun2 pun3 pun4 eva1 eva2 eva3
1        11       1     1 punishment     0 homogenous   NA    0    0    0    0    0    0
2        12       2     1 punishment     0 homogenous    0   NA    0    0    0    0    0
3        13       3     1 punishment     0 homogenous    0    0   NA    0    0    0    1
4        14       4     1 punishment     0 homogenous    0    0    1   NA    0    0    0
5        11       1     1 punishment     1 homogenous   NA    0    0    0    0    0    0
6        12       2     1 punishment     1 homogenous    0   NA    0    0    0    0    0
7        13       3     1 punishment     1 homogenous    0    0   NA    0    0    0    0
8        14       4     1 punishment     1 homogenous    0    0    0   NA    0    0    0
9        11       1     1 punishment     2 homogenous   NA    0    0    0    0    0    0
10       12       2     1 punishment     2 homogenous    0   NA    0    0    0    0    0
11       13       3     1 punishment     2 homogenous    0    0   NA    0    0    0    0
12       14       4     1 punishment     2 homogenous    0    0    0   NA    0    0    0
``````

#### 1 个答案:

``````df5\$eva_out_1 <- df5\$eva_out_2 <- NA

cond1 <- df5\$treatment == "mixed" & (is.na(df5\$pun1) | is.na(df5\$pun2))
df5\$eva_out_1[cond1] <- df5\$eva3[cond1]
df5\$eva_out_2[cond1] <- df5\$eva4[cond1]

cond2 <- df5\$treatment == "mixed" & (is.na(df5\$pun3) | is.na(df5\$pun4))
df5\$eva_out_1[cond2] <- df5\$eva1[cond2]
df5\$eva_out_2[cond2] <- df5\$eva2[cond2]
``````