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

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"

Related

Change date to the last day of month using days_in_month [duplicate]

This question already has answers here:
Create end of the month date from a date variable
(9 answers)
Closed 1 year ago.
I have a character variable and I want to change it to a variable of class Date and change it to the last day of the month using days_in_month().
My character variable is currently in the form Year-Mo, e.g. 2017-01, 2017-02.
How would I do this using as.Date and days_in_month
This is a great question, because it's been asked many times!
Check out these answers:
R calculate month end
Create end of the month date from a date variable
The great part? Packages update all the time, so sometimes the answers no longer work. That's not the case here.
#Edgar Santos, at the first link, gave a detailed answer that fits your question perfectly.
library(lubridate)
dt <- c("2017-01","2017-02") # data
dt <- ym(dt) # change to date of year-month format
day(dt) <- days_in_month(dt) # update the 'day' in the date
[1] "2017-01-31" "2017-02-28"
vec <- c("2017-01", "2017-02")
dates <- as.POSIXlt(as.Date(paste(vec, "01"), format = "%Y-%m %d"))
dates$mon <- dates$mon + 1
dates$mday <- dates$mday - 1
as.Date(dates)
# [1] "2017-01-31" "2017-02-28"

Exclude weekdays while subtracting the dates [duplicate]

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

Change dates to first day of the month in R [duplicate]

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

Convert integer YYYYWW to date [duplicate]

This question already has answers here:
Transform year/week to date object
(2 answers)
Closed 4 years ago.
The variable I want to convert is an integer in the form of YYYYWW.
So, for example 200901 represents week 1 of 2009, 201223 --> 23 of 2012 and so on.
I want to convert this variable into date format based on weeks in a year.
So in my example 01-2009 and 23-2012 or a similar format. I already tried several lubridate and ISOweek functions but never come up with a good result.
I really appreciate your help.
Not sure if it works for weeks but try the solution here:
https://stackoverflow.com/a/29928301/5335354
Something like:
df <- transform(df, x = as.Date(as.character(x), "%Y%U"))

How to get week starting date from a date in R [duplicate]

This question already has answers here:
How to find Previous Sunday in R
(4 answers)
Closed 5 years ago.
I have a dataset with a column containing dates. I want to find the week starting dates for those date values.
I get the week number using week function from lubridate.
For example,
week(as.Date("04/20/2017", "%m/%d/%Y"))
#Solution
[1] 16
Instead of weeknum, is there a way to get the starting date of the week? In this case I am expecting either "04/16/2017" or "04/17/2017". I am not very particular if the week starts from Sunday or Monday.
I looked at this question, but didn't get much from it.
Use the floor_date function from the lubridate package.
library("lubridate")
floor_date(as.Date("04/20/2017", "%m/%d/%Y"), unit="week")
You can use below
as.Date(format(as.Date("04/20/2017", "%m/%d/%Y"),"%Y-%W-1"),"%Y-%W-%u")
[1] "2017-04-17"

Resources