0

I'm trying to calculate the student's final score and using that score to give a grade based on the if statements below then append the two columns on the data frame. However, for the symbol, It's append 'B-' for all the grades. Why is that, and how can I fix it?

df = data.frame(rio_csv)
df$final_grade <- (df[,4]*0.1 +df[,5]*0.1 + df[,6]*0.1 + df[,7]*0.3 + df[, 8]*0.3)


x <- df[,9]     # Assigning the values from the 9th column to x

for(i in length(x)){      # looping through the x
  if (x[i] == 0 | x[i] < 50)df$Symbol <- "E"
  else if (x[i] == 50 | x[i] < 60)df$Symbol <- "D"
  else if (x[i] == 60 | x[i] < 63)df$Symbol <- "C-"
  else if (x[i] == 63 | x[i] < 67)df$Symbol <- "C"
  else if (x[i] == 67 | x[i] < 70)df$Symbol <- "C+"
  else if (x[i] == 70 | x[i] < 73)df$Symbol <- "B-"
  else if (x[i] == 73 | x[i] < 77)df$Symbol <- "B"
  else if (x[i] == 77 | x[i] < 80)df$Symbol <- "B+"
  else if (x[i] == 80 | x[i] < 85)df$Symbol <- "A-"
  else if (x[i] == 85 | x[i] < 90)df$Symbol <- "A"
  else df$Symbol <- "A+"
}

View(df)
Mqo
  • 1
  • 1
    Please consider using `dplyr::case_when`. See [here](https://stackoverflow.com/questions/4622060/case-statement-equivalent-in-r). – Ian Campbell Jun 15 '22 at 22:10
  • 1
    Everytime you run `df$Symbol <- "A"` you are setting the value in every row to "A" since you are not subetting df$Symbol. But see the duplicate questions for much better ways to do this. – MrFlick Jun 15 '22 at 22:13

0 Answers0