How to Determine If Date Is a Weekend or Not (Not Using Lubridate)

How to determine if date is a weekend or not (not using lubridate)

I put @AnandaMahto's suggestion here rather than a comment:

library(chron)
x <- seq(Sys.Date()-10, Sys.Date(), by = 1)
x[is.weekend(x)]

## [1] "2014-10-11" "2014-10-12" "2014-10-18"

R - exclude weekends from time interval calculations with lubridate

Probably, there is a function somewhere already doing this but here is a custom one which can help you calculate date difference excluding weekends.

library(dplyr)
library(purrr)

date_diff_excluding_wekeends <- function(x, y) {
if(is.na(x) || is.na(y)) return(NA)
sum(!format(seq(x, y - 1, by = '1 day'), '%u') %in% 6:7)
}

datecalculation %>%
mutate(Days = map2_dbl(lag(Dates), Dates, date_diff_excluding_wekeends))

# Dates Days Weekday
#1 2021-01-01 NA 6
#2 2021-01-04 1 2
#3 2021-01-05 1 3
#4 2021-01-06 1 4
#5 2021-01-11 3 2
#6 2021-01-13 2 4
#7 2021-01-14 1 5
#8 2021-01-18 2 2
#9 2021-01-25 5 2
#10 2021-01-29 4 6
  • seq(x, y - 1, by = '1 day') creates a sequence of dates between previous date and current date - 1.
  • format(..., "%u") returns day of the week. 1 is for Monday, 7 for Sunday.
  • Using sum(!format(...) %in% 6:7) we count number of days that are present on weekdays.

weekend dates within an interval R

why don't you prefer a seq of dates rather than creating the interval ? like

df$weekend.exist <- sapply(1:nrow(df), function(i) 
as.numeric(any(is.weekend(seq(df$start.date[i], df$end.date[i],by = "day")))))
# [1] 1 1

library(dplyr)
df %>%
group_by(start.date,end.date) %>%
mutate(weekend.exist = as.numeric(any(is.weekend(seq(start.date, end.date,by = "day")))))
# start.date end.date weekend.exist
# <date> <date> <dbl>
# 1 2017-01-01 2017-01-21 1
# 2 2017-02-01 2017-02-03 1

Calculate the number of weekdays between 2 dates in R


Date1 <- as.Date("2011-01-30")
Date2 <- as.Date("2011-02-04")
sum(!weekdays(seq(Date1, Date2, "days")) %in% c("Saturday", "Sunday"))

EDIT: And Zach said, let there be Vectorize :)

Dates1 <- as.Date("2011-01-30") + rep(0, 10)
Dates2 <- as.Date("2011-02-04") + seq(0, 9)
Nweekdays <- Vectorize(function(a, b)
sum(!weekdays(seq(a, b, "days")) %in% c("Saturday", "Sunday")))
Nweekdays(Dates1, Dates2)

Round date to next weekday in R

A simple solution could be using case_when from dplyr to check if weekday for that day is "Saturday" or "Sunday" and subtract the days accordingly.

library(dplyr)

df %>%
mutate(Day = weekdays(Date),
Date = case_when(Day == "Saturday" ~ Date - 1,
Day == "Sunday" ~ Date - 2,
TRUE ~ Date)) %>%
select(-Day)


# Ticker Date mean_PX_ASK mean_PX_BID Agency
#1 ABNANV 2007-03-02 102.0 102.0 Moody's
#2 ABNANV 2007-03-02 102.0 102.0 Moody's
#3 ABNANV 2007-03-12 102.0 102.0 Moody's
#4 ABNANV 2007-03-12 102.0 102.0 Moody's
#5 ABNANV 2008-09-17 88.9 88.4 Fitch
#6 ABNANV 2008-09-17 88.9 88.4 Fitch

With bizdays we need to create a calendar using create.calendar and default weekdays. We can then use adjust.previous to get the previous working day.

library(bizdays)
cal <- create.calendar("Actual", weekdays=c("saturday", "sunday"))
adjust.previous(df$Date, cal)

#[1] "2007-03-02" "2007-03-02" "2007-03-12" "2007-03-12" "2008-09-17" "2008-09-17"

R - How to determine the week ending date

Use ceiling_date in the lubridate package. You need to fiddle it a bit, since that rounds up to the next Sunday rather than Friday.

x <- seq(Sys.Date(), by = "1 day", length.out = 21)
data.frame(
x = x,
weekday = weekdays(x),
next_friday = ceiling_date(x, "week") +
ifelse(weekdays(x) %in% c("Saturday", "Sunday"), 5, -2)
)
## x weekday next_friday
## 1 2014-11-17 Monday 2014-11-21
## 2 2014-11-18 Tuesday 2014-11-21
## 3 2014-11-19 Wednesday 2014-11-21
## 4 2014-11-20 Thursday 2014-11-21
## 5 2014-11-21 Friday 2014-11-21
## 6 2014-11-22 Saturday 2014-11-28
## 7 2014-11-23 Sunday 2014-11-28
## 8 2014-11-24 Monday 2014-11-28
## 9 2014-11-25 Tuesday 2014-11-28
## 10 2014-11-26 Wednesday 2014-11-28
## 11 2014-11-27 Thursday 2014-11-28
## 12 2014-11-28 Friday 2014-11-28
## 13 2014-11-29 Saturday 2014-12-05
## 14 2014-11-30 Sunday 2014-12-05
## 15 2014-12-01 Monday 2014-12-05
## 16 2014-12-02 Tuesday 2014-12-05
## 17 2014-12-03 Wednesday 2014-12-05
## 18 2014-12-04 Thursday 2014-12-05
## 19 2014-12-05 Friday 2014-12-05
## 20 2014-12-06 Saturday 2014-12-12
## 21 2014-12-07 Sunday 2014-12-12


Related Topics



Leave a reply



Submit