In R, I created a new variable called wbao such that all values if this variable are NA:
l_raw_2$wbao <- NA
However, I want to convert these NAs to different categorical values (0-3) given certain conditionals with another variable. For example, if ba109___e is 1 and ba109___a is 0, then I would want wbao to be 0, not NA. I wrote the following code:
if l_raw_2$ba109___e=1 && ba109___a=="0"
{wbao=0}
but ran into the following error:
Error: unexpected symbol in "if ba109___e"
Does anyone know what I'm doing wrong? Any input regarding this would be much appreciated; thanks so much!
Updated to address the concern in your response which I had misinterpreted.
library(dplyr)
l_raw_2 <- l_raw_2 %>%
mutate(wbao = ifelse(
`ba109__e` == 1 & `ba109__a` == 0,
0,
ifelse(
<second conditional>,
1,
ifelse(
<third conditional>,
2,
3,
)
)
)
)
or
l_raw_2$wboa <- NA
l_raw_2$wbao <- ifelse(l_raw_2$ba109__e == 1 & l_raw_2$ba109__a == 0, 0, l_2_raw$wbao)
l_raw_2$wbao <- ifelse(<second conditional>, 1, l_raw_2$wbao)
...
or
library(dplyr)
l_raw_2 <- l_raw_2 %>%
mutate(
wbao = case_when(
`ba109__e` == 1 & `ba109__a` == 0 ~ 0,
<second conditional> ~ <desired output>,
...
)
)