0

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)))

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    Please read the information on posting at the top of the [tag:r] tag page and, in particular provide a reproducible example including all library statements, inputs (using `dput`) and expected output. – G. Grothendieck Mar 23 '23 at 12:14
  • Said differently, please do not post (only) an image of code/data/errors: it breaks screen-readers and it cannot be copied or searched (ref: https://meta.stackoverflow.com/a/285557 and https://xkcd.com/2116/). Please include the code, console output, or data (e.g., `data.frame(...)` or the output from `dput(head(x))`) directly. – r2evans Mar 23 '23 at 12:28
  • You have strings there, not dates or anything number-like, which makes your statement about *"giving me zeros"* confusing: it should instead be failing with `! non-numeric argument to binary operator`. If you choose to show data (classes) other than what you are actually using, you're less likely to get the help you need. – r2evans Mar 23 '23 at 12:35
  • Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (please be explicit about non-base packages), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Mar 23 '23 at 12:35

1 Answers1

0

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")
  )
dufei
  • 2,166
  • 1
  • 7
  • 18