R 条件 else if

问题描述 投票:0回答:1

在输出中 r 表示我有一个错误,即我的条件在我的第一个 if 条件中的长度 > 1

x<-0:100
x
if(x<=39){
  "Your grade is E(POOR)"
}else if(x<=49){
  "Your grade is D(BELOW AVERAGE)"
}else if(x<=59){
  "Your grade is C(AVERAGE)"
}else if(x<=69){
  "Your grade is B(ABOVE AVERAGE)"
}else{
  "CONGRATULATIONS you scored an A"
}
r if-statement conditional-statements
1个回答
0
投票

这里你的

x
是一个向量,而不是
if
条件的单个值

解决方法是迭代

1:100
中的每个值,例如,

Xval <- 0:100
for (x in Xval) {
    if (x <= 39) {
        print("Your grade is E(POOR)")
    } else if (x <= 49) {
        print("Your grade is D(BELOW AVERAGE)")
    } else if (x <= 59) {
        print("Your grade is C(AVERAGE)")
    } else if (x <= 69) {
        print("Your grade is B(ABOVE AVERAGE)")
    } else {
        print("CONGRATULATIONS you scored an A")
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.