Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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

like image 817
Cina Avatar asked Dec 16 '25 19:12

Cina


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)
like image 147
Callum Webb Avatar answered Dec 19 '25 08:12

Callum Webb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!