dimanche 21 février 2021

How to use ifelse to make a new variable in a dataframe

I am having trouble running this code (Part B) with ifelse. I keep getting an error "Unused arguments". I know there are other questions like this with answers and I saw them, but I can't figure out what my mistake is. My code is below. Any help is appreciated; I am very new to R.

#Question 3
#First make the dataframe
Age <- c(12, 12, 13, 13, 12, 13)
Quiz <- c('A', 'B', 'C', 'A', 'F', 'B')
Midterm <- c(92, 88, 78, 92, 55, 88)
Final <- c(95, 88, 75, 93, 50, 82)
School <- data.frame(Age, Quiz, Midterm, Final)
#Part A:
School$Grade <- ifelse(School$Age == 12, 6, 8)
#Part B:
School$QuizGrade <- 
  ifelse(
    (School$Quiz == 'A'),
    95, 
    ifelse(School$Quiz == 'B'), 
    85, 
    ifelse(School$Quiz == 'C'), 
    75, 
    ifelse(School$Quiz == 'D'), 
    70, 
    65)
View(School)

Aucun commentaire:

Enregistrer un commentaire