I have a dataset with the two columns urban_rural and religious. I want to create a new column based on the two specific interactions between these two columns. That is, urban_not_religious and rural_religious.
After some research on this website, I managed to create a new column based on the combination of the two columns with an ifelse statement, but only for those respondents that satisfy both conditions. For all others, I tried to write another ifelse statement, but then it returns only NAs. I could give all respondents with NAs the value of "No", but I want to keep NAs seperate to those which have valid values, but do not satisfy the conditions. So those who live in urban areas and are not religious with "Yes", all other respondents with "No", and NAs.
Here is a sample of my dataset and the code I used:
structure(list(urban_rural = structure(c(1L, 1L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Urban",
"Rural", "Refugee camp"), class = "factor"), religious = structure(c(2L,
1L, 2L, 2L, 3L, 2L, 2L, 3L, 1L, 3L, 3L, 1L, 3L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 2L,
3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 1L, 2L, 2L, 2L, 2L,
1L), .Label = c("Religious", "Somewhat religious", "Not religious"
), class = "factor")), row.names = c(NA, 50L), class = "data.frame")
dataset$urban_not_reg <- ""
dataset <- dataset %>%
mutate(urban_not_reg=ifelse((urban_rural=="Urban")&(religious=="Not religious"),"Yes",NA)) %>%
mutate(urban_not_reg=ifelse((urban_rural=="Rural")&(urban_rural=="Refugee camp")&(religious=="Religious")&(religious=="Somewhat religious"),"No",NA))