Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Determining student grade based on criteria rowwise

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading