0

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!

Kyle
  • 1
  • 1
    please provide a reproducible example – Nicso Nov 10 '22 at 19:21
  • Sorry for my ignorance, what do you mean by that/what would you be looking for? Also, I am getting a "length > 1" error as well. – Kyle Nov 10 '22 at 19:25
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It's much easier to help if we can actually copy/paste the code to test it. – MrFlick Nov 10 '22 at 21:07
  • Greetings! Usually it is helpful to provide a minimally reproducible dataset for questions here so people can troubleshoot your problems. One way of doing this is by using the `dput` function. You can find out how to use it here: https://youtu.be/3EID3P1oisg – Shawn Hemelstrand Nov 15 '22 at 03:27

1 Answers1

0

Assuming that I'm interpreting your code correctly, it looks like you have a dataframe named "food" that has a column named "did they eat cheese" which is used to eventually populate an existing field named "lunch snack" that already has its own values.

I feel like this would be a lot easier using mutate() and case_when() from the dplyr library.

library(dplyr)

food <- food %>%
  mutate(lunch_snack = case_when(
    `did they eat cheese` == "Yes" ~ "Cheese",
    TRUE ~ lunch_snack)

What this code does is it takes your dataframe "food" and updates itself by modifying the field "lunch_snack" to replace its value with "Cheese" if the "did they eat cheese field" has a value of "Yes". If it doesn't have a "Yes" then the "TRUE" on the left side of the tilde (~) functions as an "else" statement and sets the value of "lunch_snack" to itself.

As you get deeper into R, strongly consider taking a look at the tidyverse and its libraries, such as dplyr. It'll make your code easier to read and once you get used to it, it becomes a lot easier to program. Cheers!

Andrew Lee
  • 91
  • 5