Running into issues with subsetting after subtracting dates?

Advertisements I am running into issues with subsetting after subtracting dates using lubridate. I have a dataframe: customerid <- c("A1", "A1", "A2", "A2", "A3", "A3", "A3", "A4") orderdate <- c("2018-09-14", "2018-09-14", "2018-09-15", "2018-09-15", "2020-08-21", "2020-08-21","2020-08-21", "2018-08-10") returndate <- c("2018-09-15", "2018-09-18", "2018-09-20", "2019-09-15", "2021-08-20", "2020-07-21","2020-09-21", "2018-08-15") orderid <- c("1", "2", "3", "4", "5", "6", "7", "8")… Read More Running into issues with subsetting after subtracting dates?

R – Problem Storing lubridate dates in a dataframe

Advertisements When I’m trying to store date in a DF it is getting represented as a number instead of a data format. Here is my example. library(lubridate) df1 = data.frame(task = c(‘do somthing’, ‘do something else’, ‘do something more’) ,Start_date = c(NA, NA, NA)) Start_date = ymd("2023-12-01") df1$Date_Start[1] = Start_date Start_date #this returns a "2023-12-01"… Read More R – Problem Storing lubridate dates in a dataframe

Is there an easy way to create a vector following the pattern: 1st day of the month, last day of the month, 1st day of the month etc.?

Advertisements I’m trying to find an elegant way to create a vector of dates that follows a pattern like: x <- c(ymd("2000/01/01"), ymd("2000/01/31"), ymd("2000/02/01"), ymd("2000/02/28")) …and so on. So far I’ve just been doing this: library(lubridate) start <- ymd("2000/01/01") x <- c(start, rollback(start + month(1)), start + months(1), rollback(start + months(2)), start + months(2), rollback(start… Read More Is there an easy way to create a vector following the pattern: 1st day of the month, last day of the month, 1st day of the month etc.?

Check if any dates within an Interval are within any of the dates in another Interval

Advertisements I have 2 intervals of dates, and I want to see if any of the dates in interval_A are within interval_B. I am ideally looking for a dplyr solution. The data library(lubridate) interval_A <- new("Interval", .Data = c(20822400, 10454400, 42508800, 18662400, 12355200, 16243200, 10195200, 14774400, 37324800, 31276800, 27734400, 62985600, 15724800, 32054400, 21427200), start =… Read More Check if any dates within an Interval are within any of the dates in another Interval

How to determine difference in days between two dates across two columns and two rows by group?

Advertisements I am looking to determine the difference in days by groups across two columns and two rows. Essentially subtract from the End Day by the subsequent Start Day in the subsequent row and record the difference as new column in the data frame and start over when a new group (ID) has been identified.… Read More How to determine difference in days between two dates across two columns and two rows by group?

lubridate mdy format issue

Advertisements I have a date column whose date values are something like this: date = c("1/6/2022", "1/6/2022", "1/19/2022", "1/20/2022") When I try to convert it to new date column using lubridate::mdy I get: library(lubridate) date_new = lubridate::mdy(date) print(date_new) [1] "2022-01-06" "2022-01-06" "2022-01-19" "2022-01-20" # Desired output (mm-dd-yyyy or mm/dd/yyyy): [1] "01-06-2022" "01-06-2022" "01-19-2022" "01-20-2022" How… Read More lubridate mdy format issue

How to control the unit of a time difference in R?

Advertisements I calculate some durations in R as shown below. Depending of the value of the duration, the unit can be minutes, hours, days, etc.: library(lubridate) start <- "2022-08-27 10:06:58" end <- "2022-08-27 10:10:24" start <- as_datetime(start) end <- as_datetime(end) end – start # Time difference of 3.433333 mins start – Sys.time() # Time difference… Read More How to control the unit of a time difference in R?