I have a df with answers to survey questions, where df$Q57 is one of five answers:
- "" (<- blank is basically NA)
- I would never do this
- I will do this in five years
- I will do this in 10 years
- I will do this eventually
I want to create a dummy variable where:
- "" = NA
- I would never do this = 0
- I will do this in five years = 1
- I will do this in 10 years = 1
- I will do this eventually = 1
The best way I know how to do this is with a series of ifelse commands:
df$Q57_dummy <- ifelse(df$Q57 == "I would never install water control structures", 0, 1)
df$Q57_dummy <- ifelse(df$Q57 == "", NA, df$Q57_dummy)
table(df$Q57_dummy , useNA = "always")
This works, but I feel like there are cleaner ways to do this, and I was wondering if anyone had suggestions, because I will have to recode survey answers that have more than 1,0,NA outcomes. Thanks!