created new Treatment indicator using Tidyverse

I have long data that is unique at the person_id and year level. I have a binary variable "treated"... 1 for whether they receive treatment that year, and 0 if they do not receive treatment that year. I want to make a new column in which all subsequent years (meaning rows) should have a treated value of 1 after the first treated year (row) for each person.

here are my two current ideas how to create this indicator, but they are both wrong:

data %>%
group_by(person_id) %>%
mutate(treated_recoded_idea1 =  cummax(treated),
      treated_recoded_idea2 = ifelse(lag(treated, default = 0) == 1, 1, treated))

I appreciate and welcome your advices thank you. very much!

Hi @help
Try cumsum(treated) instead.

1 Like

hi thank you.

like this u mean?

data %>%
group_by(person_id) %>%
mutate(treated_recoded_idea1 =  ifelse(cumsum(treated) >= 1, 1, 0))

Just edit the first mutate line, and remove the second one.

1 Like

but that sums up all the past values that were 1 too, I believe

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.