First time asking a question, pretty new to R but am very invested in it being my future. I'm excited to grow with the community and become the best data analyst I can be! My first question revolved around the If/Else/If Else statements. I am trying to have my df print a result in a column based on the result of the column prior. For example, if it were asking if the person ate cheese, then the first column would be a simple "Yes" or NA. If they chose "Yes" they would not get the second question. The second question would ask "What snack did you eat?" And I want that column to auto fill with "Cheese" if it were answered so in the prior column. Otherwise, I want it to input the selection that they made (Fruit, Bread, Etc.).
My current code is:
colnames(food)[colnames(food) == "did they eat cheese"] <- "cheese_eater"
food$cheese_eater = as.factor(food$cheese_eater)
if(any(food$cheese_eater == "Yes")) {
food$lunch_snack = "Cheese"
} else {
food$lunch_snack = food$lunch_snack
}
Right now, it autopopulates Cheese into all columns. I imagine it has to do with the "any" statement. Also, I think that the "else" statement may be wrong.
Thanks for any advice!