In my current data frame, some students have both a math course as well as an algebra or pre-algebra course (in the below example, that is students 2 and 4).
student_courses <-
tribble(
~ student_id, ~class, ~letter_grade,
001, "math", "A",
001, "science", "A",
001, "PE", "C",
002, "math", "B",
002, "algebra", "A",
002, "science", "B",
002, "PE", "B",
003, "math", "B",
003, "science", "B",
003, "PE", "B",
004, "math", "B",
004, "pre-algebra", "B",
004, "science", "B",
004, "PE", "B"
)
I need to filter the data frame in such a way that if a student has both a math and pre-algebra/algebra grade, their math grade will be filtered out. Below is what I need the data frame to look like.
student_courses_answer <-
tribble(
~ student_id, ~class, ~letter_grade,
001, "math", "A",
001, "science", "A",
001, "PE", "C",
002, "algebra", "A",
002, "science", "B",
002, "PE", "B",
003, "math", "B",
003, "science", "B",
003, "PE", "B",
004, "pre-algebra", "B",
004, "science", "B",
004, "PE", "B"
)
I've tried grouping by student_id and then stacking filters, but this doesn't work. I'm stumped.
Any Ideas?