Obtaining last Friday's date - r

I can get today's date:
Sys.Date( )
But how do I get last Friday's date?
I tried:
library(xts)
date1 <- Sys.Date( )
to.weekly(date1 )
But this gives an error.

I think this should work:
library(lubridate)
Sys.Date() - wday(Sys.Date() + 1)

Try this:
library(zoo)
lastfri(Sys.Date())
where lastfri is the same as the one line function nextfri in the this zoo vignette, zoo quickref vignette, except that ceiling is replaced with floor. Note that lastfri is vectorized, i.e. it can take a vector of input dates and produces a vector of output dates. For example,
library(zoo)
Sys.Date()
## 2015-03-10
lastfri(Sys.Date() + 0:6)
## [1] "2015-03-06" "2015-03-06" "2015-03-06" "2015-03-13" "2015-03-13"
## [6] "2015-03-13" "2015-03-13"
Thus last Friday was March 6th and we keep getting March 6th until the day advances to to next Friday at which point the last Friday is March 13th.
Aside: Next Friday is Friday the 13th.

Here is a function that finds the last date for any day of the week:
getlastdate <- function(day) {
library(lubridate)
dates <- seq((Sys.Date()-7), (Sys.Date()-1), by="days")
dates[wday(dates, label=T)==day]
}
getlastdate("Mon")
# "2015-03-09"
Enter the day of the week in abbreviated format: i.e.
Sun Mon Tues Wed Thurs Fri Sat

Last Friday was 4 days ago, thus:
Sys.Date()-4
> Sys.Date()-4
[1] "2015-03-06"
OR for any day of the week, using base:
Sys.Date()-(as.POSIXlt(Sys.Date())$wday+2)

Related

How to convert ARIMA forecast output to Date

I have got the output from the ARIMA forecast. But I don't know how to convert the first column to date. The output is the prediction for the last days of 2021 and I wanted to get the date or a day number of 2021. Is it possible?
Following is the first column output which I need to convert:
2021.9589 is the output for 17th Dec.
2021.9616 is the output for 18th Dec.
2021.9644 is the output for 19th Dec.
2021.9671 ... and so on....
2021.9699
2021.9726
2021.9753
2021.9781
2021.9808
2021.9836
2021.9863
2021.989
2021.9918
2021.9945
2021.9973
2022.0000
2022.0027
2022.0055
I would use date_decimal, from lubridate, it works like:
> require(lubridate)
( d <- date_decimal(2021.9589) )
[1] "2021-12-16 23:57:50 UTC"
And then use month and day functions:
> month(d)
[1] 12
> day(d)
[1] 16

How to get current week start date ( Monday ) and End Date ( Sunday) in R

How to get current week start date ( Monday ) and End Date ( Sunday) in R.
My business weeks start from Monday and ends on Sunday.
How do i retrieve Start Date and End Date from my current date.
Eg. Curent date is 18-07-2020. How to retrieve Monday Date ( 18-07-2020) and Sunday Date (19-07-2020)
My Code :
library(lubridate)
library(mailR)
library(htmlTable)
library(DBI)
todays_date <- Sys.Date()
stdt <- floor_date(todays_date, 'week') + 1
lsdt <- floor_date(todays_date, 'week') + 7
If I execute the above code on Sunday, it falls on to next week. Any workaround to make the code to run on Sunday as well, by considering Monday to Sunday as working days.
You can use lubridate's floor_date and ceiling_date with unit as "week". By default week starts on a Sunday in lubridate so to get start date as Monday we need to add 1 in floor_date.
library(lubridate)
todays_date <- as.Date('2020-07-18')
floor_date(todays_date, 'week') + 1
#[1] "2020-07-13"
ceiling_date(todays_date, 'week')
#[1] "2020-07-19"

R - how to get the end date of the week and month from a given date? [duplicate]

This question already has answers here:
Create end of the month date from a date variable
(9 answers)
Closed 3 years ago.
How, in R, to get end dates of week, fortnight(beginning date too), month, quarter and year.
Let's say, we have date is "July 15th, 2017" and below code gets beginning dates.
> (a_date <- as.Date("15-07-17", "%d-%m-%y"))
[1] "2017-07-15"
> (beginning_date_week <- as.Date(cut(a_date, "week")))
[1] "2017-07-10"
> (beginning_date_month <- as.Date(cut(a_date, "month")))
[1] "2017-07-01"
> (beginning_date_quarter <- as.Date(cut(a_date, "quarter")))
[1] "2017-07-01"
> (beginning_date_year <- as.Date(cut(a_date, "year")))
[1] "2017-01-01"
adding 7 days to beginning date of week may get me the end date of week.
But, for month addition is simply not elegant(since some months have 30 days, some 31, some 28(29 some times)) and it only gets worse for quarters and years.
ceiling_date in the lubridate package returns the first date of the following period. Subtract 1 to get the last date of the current period.
library(lubridate)
a_date <- as.Date("15-07-17", "%d-%m-%y")
ceiling_date(a_date, "week", week_start = getOption("lubridate.week.start", 1))-1
[1] "2017-07-16"
ceiling_date(a_date, "month")-1
[1] "2017-07-31"
ceiling_date(a_date, "quarter")-1
[1] "2017-09-30"
ceiling_date(a_date, "year")-1
[1] "2017-12-31"

R - update datetime to 9am of the closest working day in the future

A datetime column of my dataframe is in the POSIXct format, e.g. "2014-08-08 14:22:00".
I'd like to update such datetime values if they fall out of working hours. The target value needs to be the closest 9am of a weekday in the future. This means:
If the current value is in the early morning of a weekday, update it to 9am of the same day;
If the current value is after 5pm prior to a weekday, update it to 9am of the next day;
If the current value is between 5pm of a Friday and 5pm of a Sunday, update it to 9am of the upcoming Monday.
To simplify matters, I'm not considering holidays. All Mondays-Fridays 9am to 5pm are considered work hours.
Any suggestions how I should do it?
Here a custom function using lubridate package (you should use it for this kind of stuff):
library(lubridate)
closeest_wday <-
function(tt){
if(!wday(tt) %in% c(1,7)){
if(hour(tt)<9) {
hour(tt)<- 9
minute(tt) <- 0
second(tt) <- 0
} else if (hour(tt)>=17) {
hour(tt)<- 9
minute(tt) <- 0
second(tt) <- 0
wday(tt) <- wday(tt) + 2
if(wday(tt) %in% c(1,7)) {
wday(tt) <- 2
}
}
}else{
wday(tt) <- wday(tt)+ ifelse(wday(tt)==1,1,2)
hour(tt)<- 9
minute(tt) <- 0
second(tt) <- 0
}
tt
}
Some tests, but I think you should continue with more tests:
closeest_wday(as.POSIXct("2014-08-14 9:22:00")) ## weekday
[1] "2014-08-14 09:22:00 CEST"
> closeest_wday(as.POSIXct("2014-08-16 9:22:00")) ## friday
[1] "2014-08-18 09:00:00 CEST"
> closeest_wday(as.POSIXct("2014-08-17 10:22:00")) ## saturday
[1] "2014-08-18 09:00:00 CEST"
> closeest_wday(as.POSIXct("2014-08-15 17:22:00")) ## sunday
[1] "2014-08-18 09:00:00 CEST"

Create end of the month date from a date variable

I have a large data frame with date variables, which reflect first day of the month. Is there an easy way to create a new data frame date variable that represents the last day of the month?
Below is some sample data:
date.start.month=seq(as.Date("2012-01-01"),length=4,by="months")
df=data.frame(date.start.month)
df$date.start.month
"2012-01-01" "2012-02-01" "2012-03-01" "2012-04-01"
I would like to return a new variable with:
"2012-01-31" "2012-02-29" "2012-03-31" "2012-04-30"
I've tried the following but it was unsuccessful:
df$date.end.month=seq(df$date.start.month,length=1,by="+1 months")
To get the end of months you could just create a Date vector containing the 1st of all the subsequent months and subtract 1 day.
date.end.month <- seq(as.Date("2012-02-01"),length=4,by="months")-1
date.end.month
[1] "2012-01-31" "2012-02-29" "2012-03-31" "2012-04-30"
Here is another solution using the lubridate package:
date.start.month=seq(as.Date("2012-01-01"),length=4,by="months")
df=data.frame(date.start.month)
library(lubridate)
df$date.end.month <- ceiling_date(df$date.start.month, "month") - days(1)
df$date.end.month
[1] "2012-01-31" "2012-02-29" "2012-03-31" "2012-04-30"
This uses the same concept given by James above, in that it gets the first day of the next month and subtracts one day.
By the way, this will work even when the input date is not necessarily the first day of the month. So for example, today is the 27th of the month and it still returns the correct last day of the month:
ceiling_date(Sys.Date(), "month") - days(1)
[1] "2017-07-31"
Use timeLastDayInMonth from the timeDate package:
df$eom <- timeLastDayInMonth(df$somedate)
library(lubridate)
as.Date("2019-09-01") - days(1)
[1] "2019-08-31"
or
library(lubridate)
as.Date("2019-09-01") + months(1) - days(1)
[1] "2019-09-30"
A straightforward solution would be using the yearmonfunction with the argument frac=1 from the xts-package. frac is a number between 0 and 1 that indicates the fraction of the way through the period that the result represents.
as.Date(as.yearmon(seq.Date(as.Date('2017-02-01'),by='month',length.out = 6)),frac=1)
[1] "2017-02-28" "2017-03-31" "2017-04-30" "2017-05-31" "2017-06-30" "2017-07-31"
Or if you prefer “piping” using magrittr:
seq.Date(as.Date('2017-02-01'),by='month',length.out = 6) %>%
as.yearmon() %>% as.Date(,frac=1)
[1] "2017-02-28" "2017-03-31" "2017-04-30" "2017-05-31" "2017-06-30" "2017-07-31"
A function as below would do the work (assume dt is scalar) -
month_end <- function(dt) {
d <- seq(dt, dt+31, by="days")
max(d[format(d,"%m")==format(dt,"%m")])
}
If you have a vector of Dates, then do the following -
sapply(dates, month_end)
you can use timeperiodsR
date.start.month=seq(as.Date("2012-01-01"),length=4,by="months")
df=data.frame(date.start.month)
df$date.start.month
# install.packages("timeperiodsR")
pm <- previous_month(df$date.start.month[1]) # get previous month
start(pm) # first day of previous month
end(pm) # last day of previous month
seq(pm) # vector with all days of previous month
We can also use bsts::LastDayInMonth:
transform(df, date.end.month = bsts::LastDayInMonth(df$date.start.month))
# date.start.month date.end.month
# 1 2012-01-01 2012-01-31
# 2 2012-02-01 2012-02-29
# 3 2012-03-01 2012-03-31
# 4 2012-04-01 2012-04-30
tidyverse has added the clock package in addition to the lubridate package that has nice functionality for this:
library(clock)
date_build(2012, 1:12, 31, invalid = "previous")
# [1] "2012-01-31" "2012-02-29" "2012-03-31" "2012-04-30" "2012-05-31" "2012-06-30"
# [7] "2012-07-31" "2012-08-31" "2012-09-30" "2012-10-31" "2012-11-30" "2012-12-31"
The invalid argument specifies what to do with an invalid date (e.g. 2012-02-31). From the documentation:
"previous": The previous valid instant in time.
"previous-day": The previous valid day in time, keeping the time of
day.
"next": The next valid instant in time.
"next-day": The next valid day in time, keeping the time of day.
"overflow": Overflow by the number of days that the input is invalid
by. Time of day is dropped.
"overflow-day": Overflow by the number of days that the input is
invalid by. Time of day is kept.
"NA": Replace invalid dates with NA.
"error": Error on invalid dates.

Resources