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]
Related
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
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
})
This question already has answers here:
How to Parse Year + Week Number in R?
(3 answers)
Get the difference between dates in terms of weeks, months, quarters, and years
(9 answers)
Closed 3 years ago.
How to find the interval between two dates that are in year and week format. Example 201630 and 201851
As #thelatemail pointed out if you have week number you also need day of the week to get the date. We can use %U to identify week number and %u to get day of the week. We use an arbitrary day of the week (here 1).
d1 <- as.character(201630)
d2 <- as.character(201851)
diff(as.Date(paste0(c(d1,d2),"1"), format="%Y%U%u"))
#Time difference of 882 days
Of if you need output in another unit we can also use difftime
difftime(as.Date(paste0(d2, 1), "%Y%U%u"),
as.Date(paste0(d1, 1), "%Y%U%u"), units = "weeks")
This question already has answers here:
Changing date format in R
(7 answers)
How to round floor_date() to arbitrary date?
(1 answer)
Change day of the month in a Date to first day (01)
(5 answers)
Closed 3 years ago.
How do I change my dates to the first day of the month...so 3/31/19 becomes 3/1/19, 2/28/19 becomes 2/1/19, and so on.
Thanks
One of the many ways to do it is to use lubridate package.
library(lubridate)
date <- dmy('11/02/2019')
day(date) <- 1
date
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