Questions tagged [lubridate]

lubridate is an R package that makes it easier to work with dates and time objects.

The lubridate package facilitates working with dates and times in . lubridate also expands the type of mathematical operations that can be performed with date-time objects. It introduces three new time span classes borrowed from :

  • durations, which measure the exact amount of time between two points
  • periods, which accurately track clock times despite leap years, leap seconds, and day light savings time
  • intervals, a protean summary of the time information between two points

Repositories

Vignettes

Other resources

Related tags

2520 questions
27
votes
7 answers

How to determine if date is a weekend or not (not using lubridate)

I have a vector of date objects (yyyy-mm-dd) and I want to determine if any of them are on weekend or not. Is there a function that can determine this straightaway? I can use wday() in the lubridate package and then determine if returned value is…
DonDyck
  • 1,451
  • 5
  • 20
  • 35
26
votes
2 answers

Find day of year with the lubridate package in R

I'm looking to find the day of year for a POSIXct class object with lubridate. For example, 12-9-2015 is day 343. It's easy to find the day of the week or month with lubridate: > lubridate::wday("2015-12-09 04:27:56 EST", labels = T) Wed >…
Joshua Rosenberg
  • 4,014
  • 9
  • 34
  • 73
26
votes
3 answers

Add (subtract) months without exceeding the last day of the new month

I am looking to add and subtract six months (bond time) reliably with lubridate. For example, adding six months to 12/31/2014 should result in 6/30/2015, and adding to 2/28/2014 should result in 8/31/2014 The issue with as.Date("2014-12-31") +…
Michael Clinton
  • 635
  • 1
  • 6
  • 12
26
votes
2 answers

Why are lubridate functions so slow when compared with as.POSIXct?

As the title goes. Why is the lubridate function so much slower? library(lubridate) library(microbenchmark) Dates <- sample(c(dates = format(seq(ISOdate(2010,1,1), by='day', length=365), format='%d-%m-%Y')), 50000, replace =…
RJ-
  • 2,919
  • 3
  • 28
  • 35
25
votes
3 answers

Have lubridate subtraction return only a numeric value

I have one variable called Started which is the date on which human subjects enrolled in a study and another variable called dos1 which is the date upon which the subject last had surgery. I want to work out how many months since their last surgery…
Farrel
  • 10,244
  • 19
  • 61
  • 99
23
votes
4 answers

Efficient and accurate age calculation (in years, months, or weeks) in R given birth date and an arbitrary date

I am facing the common task of calculating the age (in years, months, or weeks) given the date of birth and an arbitrary date. The thing is that quite often I have to do this over many many records (>300 millions), so performance is a key issue…
Hernando Casas
  • 2,837
  • 4
  • 21
  • 30
23
votes
3 answers

Convert date time to a formatted time string

I don't know why it doesn't work. Here is my code: > t <- hms("14:11:49") > t [1] "14H 11M 49S" t <- t + minutes(3) > format(t, format="%H:%M:%S") [1] "14H 14M 49S" # Expected output: "14:14:49" Update: Currently I found this solution, but I hope…
biocyberman
  • 5,675
  • 8
  • 38
  • 50
22
votes
2 answers

Why is 365 days equal to 80000 years?

I have a lubridate interval and wanted to get the number of days as integer. However I get the following strange intermediate results: library("lubridate") i1 <- interval("2015-01-01 00:00:00", "2016-01-01 00:00:00") i1 <-…
Christoph
  • 6,841
  • 4
  • 37
  • 89
21
votes
6 answers

How to flatten / merge overlapping time periods

I have a large data set of time periods, defined by a 'start' and and an 'end' column. Some of the periods overlap. I would like to combine (flatten / merge / collapse) all overlapping time periods to have one 'start' value and one 'end' value. Some…
Jonno Bourne
  • 1,931
  • 1
  • 22
  • 45
20
votes
2 answers

Transform year/week to date object

String contains 'YEAR WEEK' and I want to transform it with parse_date_time() to a date object but I can't make the code work: parse_date_time(c("201510"), "YW") I don't have to use lubridate, can be other packages, too.
Sebastian
  • 2,430
  • 4
  • 23
  • 40
18
votes
2 answers

best practices for avoiding roundoff gotchas in date manipulation

I am doing some date/time manipulation and experiencing explicable, but unpleasant, round-tripping problems when converting date -> time -> date . I have temporarily overcome this problem by rounding at appropriate points, but I wonder if there are…
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
16
votes
2 answers

R subtracting 1 month from today's date gives NA

I have a script in which I subset my data according to some set time periods and wanted to subset all the records that had occurred in the last month. However if I try to subtract one month from today's date it yields an NA: > today <- Sys.Date() >…
Amy M
  • 967
  • 1
  • 9
  • 19
16
votes
1 answer

Increase number of axis ticks in ggplot2 for dates

I have some time series data to plot and I'd like more granular axis ticks. For example: library(lubridate) library(ggplot2) library(scales) dat <- data.frame(date = paste0("09-01-", gsub(" ", "0", format(1:30))), stringsAsFactors…
topepo
  • 13,534
  • 3
  • 39
  • 52
16
votes
2 answers

Calculating Time Difference between two columns

After converting factors in POSIXCT format and then applying datetime format, I want to take the difference of datetime between 2 pos1 and pos2. However, when I do that for a specific item I get the right answer in the console but when I do the…
Shoaibkhanz
  • 1,942
  • 3
  • 24
  • 41
15
votes
7 answers

Extract time (HMS) from lubridate date time object?

I have the following datetime: t <- "2018-05-01 23:02:50 UTC" I want to split it to time and date. When I apply date(t) I get the date part. But when I use lubridate's hms, parse_date_time and other functions to do this in "HMS" order I get NA. I…
SteveS
  • 3,789
  • 5
  • 30
  • 64