I'm not sure exactly how to frame this, but I'll illustrate it. Essentially I have a survey where I ask participants which diet drinks they drink. But some participants have mistakingly put water as a diet drink, throwing off the results. The way the data is framed participants first answer "type of diet drink" (diet_drinks), then "how many per day?" (diet_drinks_amt), then "how many days per week?" (diet_drinks_amt).
What I want to do is to recode data in the diet_drinks_amt and diet_drinks_days columns to read NA where a participant put "Water" for the diet_drinks question, without removing the data for other participants who answered correctly. I know about ifelse but what I don't know how to do is recode the mistakes without erasing the correct data.
dataset <- data.frame(
diet_drinks = c("Diet Coke", "Diet Sprite", "Water"),
diet_drinks_amt = c(2,7,3),
diet_drinks_days = c(3,6,7)
)
One way I've got around the issue is to create a new variable that codes "Water" as 0 and everything else as 1, and then multiplying this variable by the diet_drinks_amt and diet_drinks_days variables, but I'm sure there must be a simpler way. Thanks in advance :)