在重新开始项目之前,我重新开始了对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
语句只允许一个条件?