So I've recently started to really dig into different function types recently and am currently working on a function that reads in a data frame, predicts two values for each row, and returns a sentence based on a combination of variables that are supposed to be flagged inside the function.But I can't get the chain of if-else statements inside the original for loop to output the correct sentence I want.
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")
}
}
}
I've been stumped the last few days trying to figure out where in this function is the origin of the problem. Thank you in advance for looking through this.
Aucun commentaire:
Enregistrer un commentaire