My participants were asked a question (cle1i2) with possible answers yes (1) or no (0).
Among these, who replied yes, were again asked another question (cle2i2), with yes (1) or no (0) possible answers.
Now, in R, I would like to create a new column (cle7_P), with values in it depending on the following conditions.
The conditions are these ones, and should be implemented all together;
-
IF cle1i2 is NA AND cle2i2 is NA, THEN cle7_P is NA;
-
IF cle1i2 is 0 AND cle2i2 is NA, THEN cle7_P is 0;
-
IF cle1i2 is 1 AND cle2i2 is 1, THEN cle7_P is 1;
-
IF cle1i2 is 1 AND cle2i2 is 0, THEN cle7_P is 0;
-
IF cle1i2 is 1 AND cle2i2 is NA, THEN cle7_P is NA;
This is what I have tried so far, but it does not work when I check manually the data.
library(dplyr)
library(tidyverse)
h$pcle7_P %>%
(ifelse(is.na(cle1i2) & is.na(cle2i2), NA, ifelse(cle1i2 == 0 & is.na(cle2i2), 0, ifelse(cle1i2 == 1 & cle2i2 == 1, 1, ifelse(cle1i2 == 1 & cle2i2 == 0, 0, ifelse(cle1i2 == 1 & is.na(cle2i2), NA))))))
h <- h %>%
mutate(pcle7_P = ifelse(cle1i2 == NA | cle2i2 == NA, NA, ifelse(cle1i2 == 0 | cle2i2 == NA, 0, ifelse(cle1i2 == 1 | cle2i2 == 1, 1, ifelse(cle1i2 == 1 | cle2i2 == 0, 0, ifelse(cle1i2 == 1 | cle2i2 == NA), NA)))))
I would expect a new column with values according to the conditions.
Can you help me? Thank you
Aucun commentaire:
Enregistrer un commentaire