I'm trying to create one variable out of multiple others. So I first created factor variables for each of the different variables as shown below:
usl <- mutate(usl, unsafenat1_fct = case_when(unsafenat1 == 0 ~ "Not mentioned", unsafenat1 == 1 ~ "Yes mentioned"), unsafenat1_fct = as.factor(unsafenat1_fct))
usl <- mutate(usl, unsafenat2_fct = case_when(unsafenat2 == 0 ~ "Not mentioned", unsafenat2 == 1 ~ "Yes mentioned"), unsafenat2_fct = as.factor(unsafenat2_fct))
And then out of these (and multiple others) I want to create a variable as shown below:
usl <- mutate(usl, unsafenat = ifelse(unsafenat1_fct == "Yes mentioned" | unsafenat2_fct == "Yes mentioned" | unsafenat3_fct == "Yes mentioned" | unsafenat4_fct == "Yes mentioned" | unsafenat5_fct == "Yes mentioned" | unsafenat6_fct == "Yes mentioned" | unsafenat7_fct == "Yes mentioned" | unsafenat8_fct == "Yes mentioned" | unsafenat9_fct == "Yes mentioned" | unsafenat10_fct == "Yes mentioned" | unsafenat11_fct == "Yes mentioned" | unsafenat12_fct == "Yes mentioned"| unsafenat97_fct == "Yes mentioned", "Yes mentioned", "Not mentioned"), unsafenat = as.factor(unsafenat))
Basically I want that if the outcome in any of the initial variables was "Yes mentioned" then I want the outcome in my new variable to also be "Yes mentioned" but if it isn't "Yes mentioned" in none of them then I want it to be "Not mentioned". However, when I do this code it only recognizes the "Yes mentioned" and all the others are added to the NA group and I don't know why.
Here's a look at the variables:
When I tried a different coding, it become clear that the problem is that it doesn't recognize "Not mentioned" as a factor but I don't understand why.
usl$unsafeethn[usl$unsafeethn1_fct == "Not mentioned"]<-"Not mentioned"
Warning message: In
[<-.factor
(*tmp*
, usl$unsafeethnn1_fct == "Not mentioned", : invalid factor level, NA generated