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

因此,我最近开始真正地深入研究不同的函数类型,并且目前正在研究一个函数,该函数读取数据帧,为每一行预测两个值,并根据应该认为是变量的组合返回一个句子在函数内部进行了标记。但是我无法在原始for循环中获取if-else语句链来输出我想要的正确句子。

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")
}

  }
}

最近几天我很困惑,试图找出此函数在哪里出了问题。预先感谢您浏览此内容。