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.
You could calculate
rowwiseif the date is before the other date by comparing it. Then you cansumthe TRUE values returning with asummariselike this:Created on 2023-03-08 with reprex v2.0.2