Is there a code that calculates days between 2 dates in the same column

I am trying to create a new variable n that calculates the number of days between 2 dates in the same column. What I want to achieve is shown in the table below:

Table

The code I have written is giving me zeros throughout. I have checked the class and its a Date. What could be this issue.

historical_sonia <- historical_sonia %>% arrange(newdate) %>% # sort by date mutate(n = as.numeric(newdate - lag(newdate)))

>Solution :

I assume the dates in your example are character strings, rather than actual dates? Convert them with lubridate::mdy() as below:

library(tidyverse)
library(lubridate)

newdate <- c("7/15/2020", "7/16/2020", "7/17/2020", "7/20/2020", "7/21/2020")

tibble(newdate) |>
  mutate(newdate = mdy(newdate),
         n = as.numeric(lead(newdate) - newdate))
#> # A tibble: 5 × 2
#>   newdate        n
#>   <date>     <dbl>
#> 1 2020-07-15     1
#> 2 2020-07-16     1
#> 3 2020-07-17     3
#> 4 2020-07-20     1
#> 5 2020-07-21    NA

Created on 2023-03-23 with reprex v2.0.2

You can also use lubridate::time_length() to compute the days in between two dates:

tibble(newdate) |>
  mutate(
    newdate = mdy(newdate),
    n = time_length(interval(newdate, lead(newdate)), unit = "days")
  )

Leave a Reply