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

1839 questions
23
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,749
  • 2
  • 24
  • 34
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
  • 5,963
  • 3
  • 31
  • 73
21
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
  • 3,364
  • 4
  • 26
  • 61
21
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,615
  • 3
  • 18
  • 29
20
votes
4 answers

dplyr, lubridate : how to aggregate a dataframe by week?

Consider the following example library(tidyverse) library(lubridate) time <- seq(from =ymd("2014-02-24"),to= ymd("2014-03-20"), by="days") set.seed(123) values <- sample(seq(from = 20, to = 50, by = 5), size = length(time), replace = TRUE) df2 <-…
ℕʘʘḆḽḘ
  • 15,284
  • 28
  • 88
  • 180
20
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
  • 4,685
  • 6
  • 31
  • 44
19
votes
3 answers

Simplest way to extract date from timestamp

Consider the following timestamp timestamp <- ymd_hms("2011-08-10 14:00:00", tz = "Pacific/Auckland") > timestamp [1] "2011-08-10 14:00:00 NZST" What is the simplest way to get the day part 2011-08-10 from it, and making sure this day is a proper…
ℕʘʘḆḽḘ
  • 15,284
  • 28
  • 88
  • 180
19
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
  • 9,584
  • 19
  • 57
  • 95
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
  • 173,430
  • 21
  • 312
  • 389
16
votes
4 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,643
  • 1
  • 19
  • 42
14
votes
1 answer

guess_formats + R + lubridate

I'm having trouble understanding how to use the guess_formats function in lubridate. I have a vector of dates in some unknown set/order of formats. I'd like to convert them to a Date object (or at least convert as many as possible). The following…
user3915170
  • 313
  • 1
  • 7
14
votes
4 answers

Lubridate week() to find consecutive week number for multi-year periods

Within R, say I have a vector of some Lubridate dates: > Date "2012-01-01 UTC" "2013-01-01 UTC" Next, suppose I want to see what week number these days fall in: > week(Date) 1 1 Lubridate is fantastic! But wait...I'm dealing a time series with…
tumultous_rooster
  • 10,446
  • 27
  • 81
  • 140
13
votes
6 answers

Calculate part of duration that occur in each hour of day

I have a dataframe with start and end times: id start_time end_time 1 1 2018-09-02 11:13:00 2018-09-02 11:54:00 2 2 2018-09-02 14:34:00 2018-09-02 14:37:00 3 3 2018-09-02 03:00:00 2018-09-02 03:30:00 4 4 2018-09-02 03:49:00…
DanG
  • 813
  • 12
  • 25
13
votes
4 answers

Assigning Dates to Fiscal Year

I'm trying to come up with some code that will look at a date and then assign it to a fiscal year. I'm totally stuck. I have a variable that contains dates in POSIXct format: df$Date #2015-05-01 CST #2015-04-30 CST #2014-09-01 CST What I need to…
Churly Pickle
  • 263
  • 4
  • 14
13
votes
4 answers

Summing rows by month in R

So I have a data frame that has a date column, an hour column and a series of other numerical columns. Each row in the data frame is 1 hour of 1 day for an entire year. The data frame looks like this: Date Hour Melbourne Southern …
user2787386
  • 295
  • 2
  • 6
  • 20