How to count the number of dates that occur before another date in a dataset in R?

88 Views Asked by At

I am working on a dataset where I have a number of dates for two days and I want to calculate the number of days where date 2 occurs before date 1 as this would indicate an error.

Here is an example tibble of some of the dates I have (I made up the data but provided it here to help out)

# A tibble:   4 × 2
   date1      date2
   <date>     <date>    
 1 2022-05-09 2023-01-10  
 2 2022-07-21 2023-3-6    
 3 2023-03-08 2022-12-11
 4 2022-02-06 2023-02-11

As you can see, the pair of dates in the 3rd row are an error since 2023-03-08 is not before 2022-12-11. How would I go about calculating the number of times this type of error shows up (the number of times date 2 occurs before date 1)?

I would like to use tidyverse and do this in a summarise() function. But if there is an easier way to do it then that works too.

1

There are 1 best solutions below

3
Quinten On

You could calculate rowwise if the date is before the other date by comparing it. Then you can sum the TRUE values returning with a summarise like this:

library(dplyr)
df %>%
  rowwise() %>%
  mutate(error = date2 <= date1) %>%
  ungroup() %>%
  summarise(total = sum(error))
#> # A tibble: 1 × 1
#>   total
#>   <int>
#> 1     1

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