I would like to find out whether a student is “PASSED” or “FAILED,” we have to apply the following criteria:
If the student has scored greater than 200 as total marks and scored greater than 33 in all subjects, then the student is “PASSED.”
If a student has scored less than 33 in 1 or 2 subjects and total marks are greater than 200, the student has got “ER” (Essential Repeat).
If the student has scored less than 33 in more than 2 subjects or less than or equal to 200 as total marks, then the student is “FAILED.”
df <- data.frame(
student = c("a", "b", "c", "d", "e"),
subject1 = c(40, 50, 60, 70, 80),
subject2 = c(35, 45, 55, 65, 75),
subject3 = c(38, 48, 58, 68, 78),
subject4 = c(33, 34, 35, 36, 37),
subject5 = c(42, 52, 62, 72, 82)
)
Any help?
>Solution :
You can do:
library(tidyverse)
df %>%
mutate(test = apply(across(-student),
1,
function(x) case_when(sum(x) > 200 & sum(x > 33) == 5 ~ 'PASSED',
sum(x) > 200 & sum(x > 33) %in% c(1, 2) ~ 'ER',
TRUE ~ 'FAILED')))
student subject1 subject2 subject3 subject4 subject5 test
1 a 40 35 38 33 42 FAILED
2 b 50 45 48 34 52 PASSED
3 c 60 55 58 35 62 PASSED
4 d 70 65 68 36 72 PASSED
5 e 80 75 78 37 82 PASSED