mardi 6 août 2019

if and if-else statements with multiple conditions

I restarting my learning of R and working on understanding the basics before jumping into a project. I am following a few courses ran by Udemy. I am a little confused about an error I am receiving when running a multi condition if statement.

    student.df <- data.frame( name = c("Sue", "Eva", "Henry", "Jan"),
                      sex = c("f", "f", "m", "m"), 
                      years = c(21, 31, 29, 19)); student.df

    student.df$male.teen = ifelse(student.df$sex == "m" & student.df$years < 20,
    "T", "F"); student.df

    student.df$male.teen = if (student.df$sex == "f" & student.df$years < 20) {
      "T" 
    } else {
      "F"
    }

In if (student.df$sex == "f" & student.df$years < 20) { : the condition has length > 1 and only the first element will be used

From what I understand is that I cannot have more than one condition with an if statement. Though with a ifelse statement, I can have as many as I want? If this is true, why do if statements only allow one condition?

Aucun commentaire:

Enregistrer un commentaire