Skip to content

Instantly share code, notes, and snippets.

@wulab
Last active May 10, 2022 20:07
Show Gist options
  • Save wulab/476f1e0957e4ebf8a90a87af8e11567e to your computer and use it in GitHub Desktop.
Save wulab/476f1e0957e4ebf8a90a87af8e11567e to your computer and use it in GitHub Desktop.
Student grading without if/else or for loop
grade <- function(score) {
levels <- c("F", "D", "C", "B", "A")
return( levels[(score>80)+(score>60)+(score>40)+(score>20)+(score>0)] )
}
@wulab
Copy link
Author

wulab commented May 10, 2022

> grade( c(64, 52, 83, 84, 57, 98, 79, 88, 87, 61) )
 [1] "B" "C" "A" "A" "C" "A" "B" "A" "A" "B"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment