Reputation: 399
I would like to apply the following logic in R:
I know that it should be surprisingly easy, but somehow I can't get it to work.
Upvotes: 0
Views: 52
Reputation: 2997
I used lubridate to make a duration from the first time in every group to the current time, and some basic summarization to check if any values meet the criteria. Let me know if this works.
library(dplyr)
library(tibble)
library(lubridate)
df <- tribble(
~Donor.id, ~Date,
1, "2019-01-01",
1, "2019-02-03",
1, "2019-03-03",
1, "2019-06-12",
1, "2019-06-23",
2, "2019-03-01",
2, "2019-07-01",
2, "2019-08-01",
) %>%
mutate(Date = parse_date_time(Date, "%y-%m-%d"))
df %>%
group_by(Donor.id) %>%
mutate(Drop.Out = (first(Date) %--% Date)/dmonths(1) != 0
& (first(Date) %--% Date)/dmonths(1) <= 3) %>%
summarise(Drop.Out = any(Drop.Out))
Upvotes: 1