Question Details

No question body available.

Tags

r dataframe conditional-statements accumulate

Answers (2)

February 3, 2026 Score: 1 Rep: 34,119 Quality: Medium Completeness: 80%

Using purrr::accumulate to find the anchordate based on your logic (if more than 28 days, use that):

library(dplyr)

df %>% mutate(dateric = as.Date(dateric)) %>% arrange(ID, dateric, .by = ID) %>% mutate( anchordate = purrr::accumulate(dateric, ~ if (.y - .x > 28) .y else .x), diff.dd = as.numeric(dateric - lag(anchordate, default = first(dateric))), keep = ifelse(dateric == anchordate, 1, 0), IDn = rownumber(), .by = ID ) %>% select(ID, dateric, IDn, diff.dd, keep)

#> ID dateric IDn diff.dd keep #> 1 1 2018-03-09 1 0 1 #> 2 1 2018-03-21 2 12 0 #> 3 2 2003-11-30 1 0 1 #> 4 2 2003-12-16 2 16 0 #> 5 2 2004-02-05 3 67 1 #> 6 2 2004-02-10 4 5 0 #> 7 3 2006-02-23 1 0 1 #> 8 3 2006-02-24 2 1 0 #> 9 3 2014-09-25 3 3136 1

Created on 2026-02-03 with reprex v2.1.1

February 3, 2026 Score: 0 Rep: 1,153 Quality: Low Completeness: 60%

I have solved a similar problem with a recursive lambda function:

library(dplyr)

df |> mutate(dateric = as.Date(dateric), diff.dd = as.numeric(dateric - min(dateric)), IDn = 1:length(dateric), keep = ifelse(duplicated((br = \(d, m = min(d), r = 1) if (!is.na(m)) br(d, d[s 28][1], r + s) else r)(dateric)), 0, 1), .by=ID)

ID dateric diff.dd IDn keep

#1 1 2018-03-09 0 1 1 #2 1 2018-03-21 12 2 0 #3 2 2003-11-30 0 1 1 #4 2 2003-12-16 16 2 0 #5 2 2004-02-05 67 3 1 #6 2 2004-02-10 72 4 0 #7 3 2006-02-23 0 1 1 #8 3 2006-02-24 1 2 0 #9 3 2014-09-25 3136 3 1

Since you also mentioned that you tried to use a for loop, here is a solution with it:

df |> 
   mutate(dateric = as.Date(dateric),
          diff.dd = as.numeric(dateric - min(dateric)),
          IDn = 1:length(dateric),
          keep = {r=c(); m=min(dateric); for(j in dateric){if(as.numeric(j)-as.numeric(m) > 28) m = j; r= c(r, m)}; ifelse(duplicated(r), 0, 1)}, .by = ID)

ID dateric diff.dd ID_n keep

#1 1 2018-03-09 0 1 1 #2 1 2018-03-21 12 2 0 #3 2 2003-11-30 0 1 1 #4 2 2003-12-16 16 2 0 #5 2 2004-02-05 67 3 1 #6 2 2004-02-10 72 4 0 #7 3 2006-02-23 0 1 1 #8 3 2006-02-24 1 2 0 #9 3 2014-09-25 3136 3 1