Plotting dates on the x-axis and customizing labels

I am trying to make a graph of a time series, but I’m having trouble achieving my desired result. I have data from a time series from May 2021 to December 2022. Original data is by day, but for my visualization, months will suffice. I used lubridate::floor_date() and summarised the observations. The resulting data is… Read More Plotting dates on the x-axis and customizing labels

R – Problem Storing lubridate dates in a dataframe

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" df1$Date_Start[1]… 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.?

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

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 = structure(c(94953600,… 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?

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. Start_Date… Read More How to determine difference in days between two dates across two columns and two rows by group?

lubridate mdy format issue

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 can… Read More lubridate mdy format issue

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

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 of… Read More How to control the unit of a time difference in R?

How to create variables in a dataframe as there are days between two dates?

In a data.frame, I would like to create as many variables as there are days between two dates. Here’s my example below. Many thanks in advance ! mydf <- data.frame( ident = c("a","b","c"), start = c("2023-01-01","2023-01-06","2023-01-24"), end = c("2023-01-03","2023-01-12","2023-01-30") ) ident start end 1 a 2023-01-01 2023-01-03 2 b 2023-01-06 2023-01-12 3 c 2023-01-24 2023-01-30… Read More How to create variables in a dataframe as there are days between two dates?