Count leap days for different date ranges in R - r

I am using R and lubridate.
I need to count the number of leap days occurring in a bunch of different date ranges. I have done lots of googling but most results seem to just want to find out if certain years are leap years but do not consider where you are starting and ending within each year, or are for different programs I am not familiar with.
I was thinking a function would be the best way to go but was struggling on getting the code down.
My idea was to count the number of leap years in the date range using lubridate's leap_year function, and then check the partial years at the beginning and end of the period and add/subtract to the leap year count if needed.
start_date <- as.Date("2008-03-31")
end_date <- as.Date("2020-09-30")
years_list <- seq(start_date, end_date, by="years")
leap_days <- sum(leap_year(years_list))
The next step would be to check the partial years and add/subtract from leap_days when needed, which is where I am struggling. The desired result for this situation would be 3 (leap years in 2012, 2016, and 2020). Ultimately, I would be checking lots of different date ranges, not just this one.
Any help is appreciated.

If you accept the premise that a "leap day" is always February 29, then perhaps
grep("-02-29", seq(start_date, end_date, by = "day"), value = TRUE)
# [1] "2012-02-29" "2016-02-29" "2020-02-29"

This function seems to work, returning the total count of leap days.
count_leap_days <- function(x){
if(!require(lubridate)){
stop("install package 'lubridate'")
}
first_leap <- if(leap_year(x[1])) month(x[1]) %in% 1:2
x <- x[-1]
n <- length(x)
if(n > 0){
if(leap_year(x[n])) {
last_leap <- (month(x[n]) >= 3) || (month(x[n] == 2 && day(x[n] == 29)))
x <- x[-n]
}
}
ly <- c(first_leap, leap_year(x), last_leap)
sum(ly)
}
count_leap_days(years_list)
#[1] 3

Related

Get the month from the week of the year

Let's say we have this:
ex <- c('2012-41')
This represent the week 41 from the year 2012. How would I get the month from this?
Since a week can be between two months, I will be interested to get the month when that week started (here October).
Not duplicate to How to extract Month from date in R (do not have a standard date format like %Y-%m-%d).
you could try:
ex <- c('2019-10')
splitDate <- strsplit(ex, "-")
dateNew <- as.Date(paste(splitDate[[1]][1], splitDate[[1]][2], 1, sep="-"), "%Y-%U-%u")
monthSelected <- lubridate::month(dateNew)
3
I hope this helps!
This depends on the definition of week. See the discussion of %V and %W in ?strptime for two possible definitions of week. We use %V below but the function allows one to specify the other if desired. The function performs a sapply over the elements of x and for each such element it extracts the year into yr and forms a sequence of all dates for that year in sq. It then converts those dates to year-month and finds the first occurrence of the current component of x in that sequence, finally extracting the match's month.
yw2m <- function(x, fmt = "%Y-%V") {
sapply(x, function(x) {
yr <- as.numeric(substr(x, 1, 4))
sq <- seq(as.Date(paste0(yr, "-01-01")), as.Date(paste0(yr, "-12-31")), "day")
as.numeric(format(sq[which.max(format(sq, fmt) == x)], "%m"))
})
}
yw2m('2012-41')
## [1] 10
The following will add the week-of-year to an input of year-week formatted strings and return a vector of dates as character. The lubridate package weeks() function will add the dates corresponding to the end of the relevant week. Note for example I've added an additional case in your 'ex' variable to the 52nd week, and it returns Dec-31st
library(lubridate)
ex <- c('2012-41','2016-4','2018-52')
dates <- strsplit(ex,"-")
dates <- sapply(dates,function(x) {
year_week <- unlist(x)
year <- year_week[1]
week <- year_week[2]
start_date <- as.Date(paste0(year,'-01-01'))
date <- start_date+weeks(week)
#note here: OP asked for beginning of week.
#There's some ambiguity here, the above is end-of-week;
#uncommment here for beginning of week, just subtracted 6 days.
#I think this might yield inconsistent results, especially year-boundaries
#hence suggestion to use end of week. See below for possible solution
#date <- start_date+weeks(week)-days(6)
return (as.character(date))
})
Yields:
> dates
[1] "2012-10-14" "2016-01-29" "2018-12-31"
And to simply get the month from these full dates:
month(dates)
Yields:
> month(dates)
[1] 10 1 12

Calculate time difference on only months and days

I would like to calculate the time difference considering ONLY the days and months.
For example:
> as.Date("2018-12-15")-as.Date("2018-12-16")
Time difference of -1 days
> as.Date("2008-12-15")-as.Date("2018-12-16")
Time difference of -3653 days
I want both of them to return -1.
Edit:
Leap years should not be considered as we just want an approximation and the return value do not need to be exact.
As suggested by #Omry Atia we can set the years component to same year and then calculate the difference.
library(lubridate)
get_difference_without_years <- function(x, y) {
x <- ymd(x)
year(x) <- 2018
y <- ymd(y)
year(y) <- 2018
x - y
}
get_difference_without_years("2018-12-15", "2018-12-16")
#Time difference of -1 days
get_difference_without_years("2008-12-15", "2018-12-16")
#Time difference of -1 days
To keep it in base R
get_difference_without_years <- function(x, y) {
x <- as.Date(paste0("2018-", format(as.Date(x), "%m-%d")))
y <- as.Date(paste0("2018-", format(as.Date(y), "%m-%d")))
x - y
}
get_difference_without_years("2008-12-15", "2018-12-16")
#Time difference of -1 days
get_difference_without_years("2018-12-15", "2018-12-16")
#Time difference of -1 days
The question is not well defined for the case that the dates straddle the end of Feb and one year is a leap year and one is not but ignoring this we can replace the year in each date with a leap year if either is a leap year (year 2000) and a non-leap year (year 1999) otherwise and then subtract:
library(lubridate)
d1 <- "2008-12-15"
d2 <- "2018-12-16"
yr <- 1999 + (leap_year(as.Date(d1)) || leap_year(as.Date(d2)))
as.Date(sub("....", yr, d1)) - as.Date(sub("....", yr, d2))
## Time difference of -1 days
ADDED
In a comment the poster indicated that we can ignore the problems introduced by leap years. In that case we can just pick a leap year as the date to substitute in so that it always returns an answer. We do that below. We no longer need lubridate to check whether the dates are leap years or not.
as.Date(sub("....", 2000, d1)) - as.Date(sub("....", 2000, d2))
## Time difference of -1 days
(Alternately we could pick a year that is not a leap year and since most years are not leap years that would more likely not be one day off for straddled dates; however, it would be at the cost of failing if one of the dates is Feb 29th.)
If we're allowed to be a bit more approximate, ignoring leap-years, we can simplify things a bit by using %j (day of year) in format().
yd_diff <- function(x, y=NULL) {
x <- as.integer(format(x, "%j"))
if (is.null(y)) {
diff(x)
} else {
x - as.integer(format(y, "%j"))
}
}
d1 <- as.Date("2008-12-15")
d2 <- as.Date("2018-12-16")
yd_diff(d1, d2)
# 0
set.seed(1)
rd <- as.Date(sample(1:10000, 5), origin="1970-01-01")
yd_diff(rd)
# -30 180 65 -123
And even simpler, we can convert the date to integer and take the modulo days in a year. Graciously, R lets you use modulo with non-integers.
(as.integer(d1) %% 365.24) - (as.integer(d2) %% 365.24)
# -0.6
diff(as.integer(rd) %% 365.24)
# -30.72 180.80 64.84 -123.44
Another solution might be to extract only the day-of-year from each date, and then do the maths op, especially if leap-years are important.
For example, the DoY for the following:
DayOfYear(2020, 12, 15) = 350 # leap year
DayOfYear(2018, 12, 15) = 349
DayOfYear(2016, 12, 15) = 350 # leap year
DayOfYear(2011, 12, 16) = 350
You can find lots of suggestions on how to get the DoY from extract day number of year from dates and How do you convert POSIX date to day of year in R?.

How to convert words to a date?

I have a df of dates that are in this format: 4 days ago,
6 weeks ago, 8 months ago, 1 year ago.
I want to write a statement that checks first to see if it's month, week, year. Then it extracts the number. After that I do the appropriate calculation by subtracting from Sys.Date(). I've tried a couple different ways and can't get it to work.
Any chance you can help me with one and I can i figure out rest?
Thanks in advance.
Does this crude function help you? It should work even for strings like "3 years, 2 months ago". Returns NA if month, year or day do not appear in the string with a number in front.
library("stringr")
# Small helper function to convert NAs to zero and convert to numeric
na_to_zero <- function(x) {
x[is.na(x)] <- "0"
return(as.numeric(x))
}
get_date_before_today <- function(d) {
today <- Sys.Date()
days <- na_to_zero(str_extract(d, "(?i)[0-9]*(?= day\\D)"))
months <- na_to_zero(str_extract(d, "(?i)[0-9]*(?= month\\D)"))
years <- na_to_zero(str_extract(d, "(?i)[0-9]*(?= year\\D)"))
days_ago <- days + 365.25/12*months + 365.25*years
date_before_today <- today - days_ago
# If no matches were made, zeros are substituted for all, and hence days_ago is 0
date_before_today[days_ago == 0] <- NA
return(date_before_today)
}
Testing:
d <- c("4 months ago asds", "2 years ago", "1 day ago", "5 years, 3 months", "never")
get_date_before_today(d)
#[1] "2018-05-15" "2016-09-13" "2018-09-13" "2013-06-14" NA
Note, it does not give you exact dates per se. But I guess one can argue that, for example, 1 month ago can be ambiguous. What does 1 month ago mean exactly for if today is the 31st of October?
The "weeks" case can be added trivially.
We can patch together a few tidyverse functions to make quick work of this. Mostly using lubrdate for the date shifting, stringr for the string parsing, and purrr for the mapping. For example
mm <- stringr::str_match(x, "(\\d+) (day|week|month|year)s? ago")
shifter <- list(day=days, week=weeks, month=months, year=years)
shifts <- map2(mm[,3], as.numeric(mm[,2]), ~case_when(.x=="day"~days(.y),
.x=="week"~weeks(.y),
.x=="month"~months(.y),
.x=="year"~years(.y)))
map_dbl(shifts, ~today()-.x) %>% as_date
# [1] "2018-09-10" "2018-08-03" "2018-01-14" "2017-09-14"
# where today() returns [1] "2018-09-14"

How to create sequence of date for different calendars(365 days) in R?

I have .nc data file which contains daily temperature data for complete period in a single column.Time variable have 365 days calendar(no-leap year). I want to calculate monthly/ annually mean/max/min values.How to use as.date for 365 days calander?
d<- seq(as.Date("2071-01-01"), as.Date("2099-12-31"),1)
The above code create the sequence but it includes the leap year also.
I'am not sure if it is the nicest way of doing it. I would remove all 29th of february's from the sequence.
d <- d[!grepl(x = d, pattern = "-02-29$")]
Not sure whether you want to remove all the leap years or remove just the feb 29s
x<-seq(as.Date("2011-01-01"),as.Date("2016-03-01"),by="day")
is.leapyear=function(year) return(((year %% 4 == 0) & (year %% 100 != 0)) | (year %% 400 == 0))
#remove all dates in leap years
x[!is.leapyear(as.integer(format(x, "%Y")))]
#remove all feb 29s
x[format(x,"%m-%d") != "02-29"]

Get date difference in years (floating point)

I want to correct source activity based on the difference between reference and measurement date and source half life (measured in years). Say I have
ref_date <- as.Date('06/01/08',format='%d/%m/%y')
and a column in my data.frame with the same date format, e.g.,
today <- as.Date(Sys.Date(), format='%d/%m/%y')
I can find the number of years between these dates using the lubridate package
year(today)-year(ref_date)
[1] 5
Is there a function I can use to get a floating point answer today - ref_date = 5.2y, for example?
Yes, of course, use difftime() with an as numeric:
R> as.numeric(difftime(as.Date("2003-04-05"), as.Date("2001-01-01"),
+ unit="weeks"))/52.25
[1] 2.2529
R>
Note that we do have to switch to weeks scaled by 52.25 as there is a bit of ambiguity
there in terms of counting years---a February 29 comes around every 4 years but not every 100th etc.
So you have to define that. difftime() handles all time units up to weeks. Months cannot be done for the same reason of the non-constant 'numerator'.
The lubridate package contains a built-in function, time_length, which can help perform this task.
time_length(difftime(as.Date("2003-04-05"), as.Date("2001-01-01")), "years")
[1] 2.257534
time_length(difftime(as.Date("2017-03-01"), as.Date("2012-03-01")),"years")
[1] 5.00274
Documentation for the lubridate package can be found here.
Inspired by Bryan F, time_length() would work better if using interval object
time_length(interval(as.Date("2003-04-05"), as.Date("2001-01-01")), "years")
[1] -2.257534
time_length(difftime(as.Date("2017-03-01"), as.Date("2012-03-01")),"years")
[1] 5.00274
time_length(interval(as.Date("2017-03-01"), as.Date("2012-03-01")),"years")
[1] -5
You can see if you use interval() to get the time difference and then pass it to time_length(), time_length() would take into account the fact that not all months and years have the same number of days, e.g., the leap year.
Not an exact answer to your question, but the answer from Dirk Eddelbuettel in some situations can produce small errors.
Please, consider the following example:
as.numeric(difftime(as.Date("2012-03-01"), as.Date("2017-03-01"), unit="weeks"))/52.25
[1] -4.992481
The correct answer here should be at least 5 years.
The following function (using lubridate package) will calculate a number of full years between two dates:
# Function to calculate an exact full number of years between two dates
year.diff <- function(firstDate, secondDate) {
yearsdiff <- year(secondDate) - year(firstDate)
monthsdiff <- month(secondDate) - month(firstDate)
daysdiff <- day(secondDate) - day(firstDate)
if ((monthsdiff < 0) | (monthsdiff == 0 & daysdiff < 0)) {
yearsdiff <- yearsdiff - 1
}
yearsdiff
}
You can modify it to calculate a fractional part depending on how you define the number of days in the last (not finished) year.
You can use the function AnnivDates() of the package BondValuation:
R> library('BondValuation')
R> DateIndexes <- unlist(
+ suppressWarnings(
+ AnnivDates("2001-01-01", "2003-04-05", CpY=1)$DateVectors[2]
+ )
+ )
R> names(DateIndexes) <- NULL
R> DateIndexes[length(DateIndexes)] - DateIndexes[1]
[1] 2.257534
Click here for documentation of the package BondValuation.
To get the date difference in years (floating point) you can convert the dates to decimal numbers of Year and calculate then their difference.
#Example Dates
x <- as.Date(c("2001-01-01", "2003-04-05"))
#Convert Date to decimal year:
date2DYear <- function(x) {
as.numeric(format(x,"%Y")) + #Get Year an add
(as.numeric(format(x,"%j")) - 0.5) / #Day of the year divided by
as.numeric(format(as.Date(paste0(format(x,"%Y"), "-12-31")),"%j")) #days of the year
}
diff(date2DYear(x)) #Get the difference in years
#[1] 2.257534
I subtract 0.5 from the day of the year as it is not known if you are at the beginning or the end of the day and %j starts with 1.
I think the difference between 2012-03-01 and 2017-03-01 need not to be 5 Years, as 2012 has 366 days and 2017 365 and 2012-03-01 is on the 61 day of the year and 2017-03-01 on the 60.
x <- as.Date(c("2012-03-01", "2017-03-01"))
diff(date2DYear(x))
#[1] 4.997713
Note that using time_length and interval from lubridate need not come to the same result when you make a cumulative time difference.
library(lubridate)
x <- as.Date(c("2012-01-01", "2012-03-01", "2012-12-31"))
time_length(interval(x[1], x[3]), "years")
#[1] 0.9972678
time_length(interval(x[1], x[2]), "years") +
time_length(interval(x[2], x[3]), "years")
#[1] 0.9995509 #!
diff(date2DYear(x[c(1,3)]))
#[1] 0.9972678
diff(date2DYear(x[c(1,2)])) + diff(date2DYear(x[c(2,3)]))
#[1] 0.9972678
x <- as.Date(c("2013-01-01", "2013-03-01", "2013-12-31"))
time_length(interval(x[1], x[3]), "years")
#[1] 0.9972603
time_length(interval(x[1], x[2]), "years") +
time_length(interval(x[2], x[3]), "years")
#[1] 0.9972603
diff(date2DYear(x[c(1,3)]))
#[1] 0.9972603
diff(date2DYear(x[c(1,2)])) + diff(date2DYear(x[c(2,3)]))
#[1] 0.9972603
Since you are already using lubridate package, you can obtain number of years in floating point using a simple trick:
find number of seconds in one year:
seconds_in_a_year <- as.integer((seconds(ymd("2010-01-01")) - seconds(ymd("2009-01-01"))))
now obtain number of seconds between the 2 dates you desire
seconds_between_dates <- as.integer(seconds(date1) - seconds(date2))
your final answer for number of years in floating points will be
years_between_dates <- seconds_between_dates / seconds_in_a_year

Resources