Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculate the lagged differences between consecutive dates in vectors

The sample dataset is given as below:

v = data.frame(group = c(1,1,2,3,3),date = as.Date(c('01-01-2000','01-01-2001','01-05-2000','02-07-2000','01-01-2008'), "%d-%m-%Y"))
  
v%>% group_by(group ) %>% mutate(difference_day = ifelse(n() == 2, 
                                                         c(0,diff(date )),
                                                         difftime(date ,as.Date('31-12-2021', "%d-%m-%Y"),units='days')))

My desirable result is :

group difference_day
1 0
1 365
2 7915
3 0
3 2740

In the above code, if the length of groups is equal to one, then the days_difference will be
difftime(date ,as.Date('31-12-2021', "%d-%m-%Y"),units='days')).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

However,
the output of the code was:

# A tibble: 5 × 3
# Groups:   group [3]
  group date       difference_day
  <dbl> <date>              <dbl>
1     1 2000-01-01              0
2     1 2001-01-01              0
3     2 2000-05-01          -7914
4     3 2000-07-02              0
5     3 2008-01-01              0

which was very strange.
Please give me some suggestions, thank you!

>Solution :

Since you want to replace either the first vector or the second vector, use if instead of if_else. (That is, your conditional is external to the vectors, not an element-by-element conditional, where if_else would be more appropriate.)

v %>%
  group_by(group) %>%
  mutate(d = if (n() == 2L) diff(c(date[1], date)) else difftime(as.Date("2021-12-31"), date, units = "days")) %>%
  ungroup()
# # A tibble: 5 × 3
#   group date       d        
#   <dbl> <date>     <drtn>   
# 1     1 2000-01-01    0 days
# 2     1 2001-01-01  366 days
# 3     2 2000-05-01 7914 days
# 4     3 2000-07-02    0 days
# 5     3 2008-01-01 2739 days

There are some differences of +/- 1 from your expected output, not sure if that was a typo or some other intent outside of a traditional diff.

The return from both diff and difftime here are class "difftime", which prints naturally with ". days"; they are still number-enough that math or such still works on them. If you prefer not, just wrap with as.integer(.) or as.numeric(.).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading