具有多个条件的if和if-else语句

时间:2019-08-07 06:07:44

标签: if-statement rstudio

在重新开始项目之前,我重新开始了对R的学习并致力于了解基础知识。我正在关注Udemy开设的一些课程。对于运行多条件if语句时收到的错误,我有些困惑。

    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

据我了解,使用if语句不能满足多个条件。尽管使用ifelse语句,我可以拥有任意多的内容吗?如果是这样,为什么if语句只允许一个条件?

0 个答案:

没有答案