# if-else链未在R中返回正确的结果吗？

``````nbapredictor <- function(x){
for(row in x) {

#I save each column from the dataset to a variable in the for loop
Year <- 2019
A <- x[,1]#Character
B <- x[,2]#Character
C <- x[,3]#Numeric
D <- x[,4]#Character
E <- x[,5]#Numeric
G <- x[,6]#POSIXct

#To get these values I use a model that is established outside of the function and uses the variables given above to make these predictions
r1 <- predict(model, data.frame(v1=A, v2=B, v6=G,Year=Year) ,type = "response")
r2 <- predict(model, data.frame(v1=A, v2=B, v6=G,Year=Year) ,type = "response")

# I then create 4 variables based off of the predicted values
x <- mutate(x, cat1 = ifelse(r1>r2,1,0))
x <- mutate(x, cat2 = ifelse(D == "+",1, ifelse(D == "-",0,"NA")))
x <- mutate(x, cat3 = ifelse(round(r1)+round(r2) > C,1, ifelse(round(r1)+round(r2) < C,0,3)))

x <- mutate(x, cat4 = ifelse((cat2==1 & (round(r1)+ E)-round(r2)>0) | (cat2==0 & (round(r1)- E)-round(r2)>0),1,   ifelse((new2==1 & (round(r1)+ E)-round(r2)==0 | (new2==0 & (round(r1)- E)-round(r2)==0)),2,0)))

#Finally I use those 4 variables to indicate which sentence I want the for loop to output for each row. The problem. I'm having is the output sentence ends up jumbled
if(cat1 ==1 & cat3 ==1 & cat4 ==1){
return(paste0(B," v ",A,": ",A," win ", round(r1),"-",round(r2),", ",A," outside",D,E," prediction", " Survived ", C))
} else if(cat1 ==1 & cat3 ==1 & cat4 ==0){
return(paste0(B," v ",A,": ",A," win ", round(r1),"-",round(r2),", ",B," outside ",D,E," prediction", " Survived ",C))
} else if(cat1 ==1 & cat3 ==0 & cat4 ==0){
return(paste0(B," v ",A,": ",A," win ", round(r1),"-",round(r2),", ",B," outside ",D,E," prediction", " abdicated ",C))
} else if(cat1 ==1 & cat3 ==0 & cat4 ==1){
return(paste0(B," v ",A,": ",A," win ", round(r1),"-",round(r2),", ",A," outside ",D,E," prediction", " abdicated ",C))
} else if(cat1 ==0 & cat3 ==1 & cat4 ==1){
return(paste0(B," v ",A,": ",B," win ", round(r2),"-",round(r1),", ",A," outside ",D,E," prediction", " Survived ",C))
} else if(cat1 ==0 & cat3 ==1 & cat4 ==0){
return(paste0(B," v ",A,": ",B," win ", round(r2),"-",round(r1),", ",B," outside ",D,E," prediction ", " Survived ",C))
} else if(cat1 ==0 & cat3 ==0 & cat4 ==0){
return(paste0(B," v ",A,": ",B," win ", round(r2),"-",round(r1),", ",B," outside ",D,E," prediction ", " abdicated ",C))
} else if(cat1 ==0 & cat3 ==0 & cat4 ==1){
return(paste0(B," v ",A,": ",B," win ", round(r2),"-",round(r1),", ",A," outside ",D,E," prediction ", " abdicated ",C))

} else{
return("ERROR")
}

}
}

``````