Exclude weekdays while subtracting the dates [duplicate] - r

This question already has answers here:
Difference between two dates excluding weekends and given list of holidays in R
(2 answers)
Closed 2 years ago.
Is there a way to exclude weekends while taking a difference between dates
as.Date("2021-02-02") - as.Date("2021-01-25")
Time difference of 8 days
Expected output (Since there are 2 days that are weekends and needs to be exlcuded
Time difference of 6 days

Create a sequence between two days, exclude weekends and count the days.
s1 <- as.Date("2021-02-02")
s2 <- as.Date("2021-01-25")
sum(!format(seq(s2, s1, 'day'), '%u') %in% 6:7) - 1
#[1] 6

Does this work, creating a function.
daysdiff <- function(date1, date2){
sum(!weekdays(seq( as.Date(date1), as.Date(date2), 'days')) %in% c('Sunday','Saturday')) - 1
}
daysdiff("2021-01-25", "2021-02-02")
[1] 6

Related

R; lubridate addition of months gives NA [duplicate]

This question already has answers here:
Add a month to a Date [duplicate]
(8 answers)
Closed 1 year ago.
I am calculating with dates in a For loop. I combine data from two dataframes. Tibble 1 contains variable A, tibble 2 contains variable B and C.
A is a numerical variable, B and C are both dates.
I want to assign variable A a new variable if date B is within the interval of date C + 16 months.
I used the following:
if (B < C + months(16)) { Df1$A = A+1 }
For some dates this does not work. For example October 30th + 16 months = february 30th. The conditional expression fails as there is no true or falls and the for loop stops.
Is there a way to change C + months(16) to the last day of the month if the specific date (february 30th in the example above) does not exist?
You can use %m+% to add 16 Months.
library(lubridate)
ymd('2000-10-30') %m+% months(16)
#[1] "2002-02-28"

How to calculate start-date from end date and number of days [duplicate]

This question already has answers here:
How to subtract/add days from/to a date?
(3 answers)
Calculate number of days between two dates in r
(4 answers)
Closed 3 years ago.
I have a column with end dates and another column with number of days. I want to calculate start date from this information.
End dates: 7/15/2019; 6/10/2019
Number of days: 10; 9
I want to calculate Start date from the above information in R.
The lubridate package provides functions to convert strings to Date (in this case, mdy). Then you can just subtract the number of days from the end date.
ed = c("7/15/2019", "6/10/2019")
n = c(10, 9)
lubridate::mdy(ed) - n
#[1] "2019-07-05" "2019-06-01"
Consider straightforward subtraction with base R:
df <- within(df, {
end_date <- as.Date(end_date, format="%m/%d/%Y")
start_date <- end_date - num_of_days
})

how can I add trading days? [duplicate]

This question already has answers here:
Adding 15 business days in lubridate
(5 answers)
add 1 business day to date in R
(3 answers)
Closed 4 years ago.
for example in a date in thee form 07/03/2015 how can I add 30 trading days??
as.Date("07/03/2015", format="%d/%m/%Y") + 30
[1] "2015-04-06"`
this take into account also Sundays and Saturdays how can I add only the trading days? thanks
Is this what you are looking for?
Create data:
library(chron)
dates <- seq.dates("07/03/2015", by = "day", length = 30)
Generate weekdays:
dates <- weekdays(as.Date(dates))
Extract the days of the week:
remove <- c('Saturday', 'Sunday')
dates [! dates %in% remove]

R go back 3 months, always month end last day [duplicate]

This question already has answers here:
How to subtract months from a date in R?
(6 answers)
Closed 4 years ago.
I am trying to get 3 month back, and tried many different solutions posted here. The one that generally works fine is dt %m+% months(-3) from this post. however, it is not working well again for this month. I wonder if there is a ultimate final working fine solution.
dt_1 <- as.Date("2018-06-30")
dt_2 <- as.character(dt_1 %m+% months(-3))
dt_2 became "2018-03-30" instead of "2018-03-31".
You can use floor_date to get to the first day of your input month, then subtract 2 months and 1 day.
library(lubridate)
dt_1 <- as.Date("2018-06-30")
as.character(floor_date(dt_1, 'month') - months(2) - 1)
#[1] "2018-03-31"

Using R to count number of specific days in a specific month [duplicate]

This question already has answers here:
Count the number of Fridays or Mondays in Month in R
(3 answers)
Closed 7 years ago.
I'm trying to count the number of Saturdays and then Sundays in a specific month within R.
So for example if you entered the month for example Feb-2014 into the function it would return 4 for Saturday and 4 for Sunday. But if you input Jan-2015 it would return 5 for Saturday and 4 for Sunday.
Any ideas?
You can use lubridate for this. Here's a simple example. You'll have to pass it actual dates (not just months, though):
library("lubridate")
weekend_days <- function(x) {
x <- as.Date(x)
d <- days_in_month(x)
d <- x + (0:(d-1))
sum(wday(d) %in% c(1,7))
}
Some examples (for January and February 2012):
weekend_days("2012-01-01")
# [1] 9
weekend_days("2012-02-01")
# [1] 8

Resources