Attach a number to string in a column based on condition in other columns in R

Viewed 133

I would like to add a number to only MATH in column course based on the following condition; for each ID if MATH has output column equal to pass increase the attached number by 1 for the next MATH. here is an example:

df = read.table(text = 
'ID  course  outcome
 10  MATH    fail
 10  PHYS    pass
 10  MATH    pass
 10  MATH    fail
 10  CHEM    pass
 11  MATH    pass
 11  PHYS    fail
 12  CMP     pass
 12  MATH    fail
 12  MATH    fail
 12  CHEM    pass
 12  MATH    pass
 12  MATH    fail', header = T)


df_result = read.table(text = 
  'ID  course  outcome
   10  MATH1    fail
   10  PHYS     pass
   10  MATH1    pass
   10  MATH2    fail
   10  CHEM     pass
   11  MATH1    pass
   11  PHYS     fail
   12  CMP      pass
   12  MATH1    fail
   12  MATH1    fail
   12  CHEM     pass
   12  MATH1    pass
   12  MATH2    fail', header = T)

Please help me to do it in R

1 Answers

Something like this should do the trick (using dplyr):

df %>% 
  group_by(ID) %>%
  mutate(math_pass_count = 1 + lag(cumsum(course == "MATH" & outcome == "pass"), 
                                   default = 0),
         suffix = ifelse(course == "MATH", math_pass_count, ""),
         course = paste0(course, suffix)) %>%
  select(-math_pass_count, -suffix)
Related