In: Computer Science
I have dataset to predict how pass or fail based on score column. If the grade is higher than 90 we predict it pass if it below we predict it fall. the class column is the actual result so based on our prediction we got 10 correct answer out of 14. I want to do this I RStudio. To show how many corrected result we got based on the class.
Score | Our_prediction | class |
77.29597 | Fail | Fail |
77.20041 | Fail | Fail |
76.67481 | Fail | Fail |
76.60455 | Fail | Pass |
76.2072 | Fail | Fail |
76.2072 | Fail | Fail |
76.15788 | Fail | Pass |
76.07912 | Fail | Fail |
76.04913 | Fail | Pass |
75.45997 | Fail | Pass |
98.42474 | Pass | Pass |
98.27169 | Pass | Pass |
97.90262 | Pass | Pass |
97.5566 | Pass | Pass |
ANSWER:
CODE TEXT
# creating score and class data using dataframe
# with score and respective class list
# storing dataframe in data
data <- data.frame(Score=c(77.29597,77.20041,76.67481,76.60455 ,76.2072,76.2072,76.15788 ,76.07912,
76.04913,75.45997,98.42474,98.27169,97.90262,97.5566),
class=c("Fail","Fail","Fail","Pass","Fail","Fail","Pass","Fail","Pass","Pass","Pass",
"Pass","Pass","Pass"))
# displaying data
print("Data:")
data
# creating prediction function, to predict pass if Score > 90
predict <- function (X){
# if X>90 return Pass
if(X>90){
"Pass"
} # else fail
else
"Fail"
}
# using function lapply to apply predict function on each Score of data and storing result in Our_prediction
# col of dataframe
data$Our_prediction <- lapply(data$Score, predict)
# displaying prediction data
print("Predicted data in Our_prediction col:")
data
# comparing prediction with actual and displaying result
# comparision will give boolean vector with true where prediction matches, using sum function to fetch
# to correct predictions
correct_pred <- sum(data$Our_prediction == data$class)
# displaying result
cat("Out of",length(data$Our_prediction),", we got",correct_pred,"correct answer.")
CODE IMAGE
OUTPUT IMAGE