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
15
votes
3 answers

Format Date to Year-Month in R

I would like to retain my current date column in year-month format as date. It currently gets converted to chr format. I have tried as_datetime but it coerces all values to NA. The format I am looking for is: "2017-01" library(lubridate) df<-…
AM_123
  • 205
  • 1
  • 2
  • 10
15
votes
2 answers

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
  • 323
  • 2
  • 7
15
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
  • 12,150
  • 32
  • 92
  • 149
14
votes
1 answer

ggplot using grouped date variables (such as year_month)

I feel like this should be an easy task for ggplot, tidyverse, lubridate, but I cannot seem to find an elegant solution. GOAL: Create a bar graph of my data aggregated/summarized/grouped_by year and month.…
Jeff Parker
  • 1,809
  • 1
  • 18
  • 28
14
votes
6 answers

Find dates that fail to parse in R Lubridate

As a R novice I'm pulling my hair out trying to debug cryptic R errors. I have csv that containing 150k lines that I load into a data frame named 'date'. I then use lubridate to convert this character column to datetimes in hopes of finding min/max…
Korben Dallas
  • 477
  • 1
  • 6
  • 16
14
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
  • 323
  • 1
  • 7
  • 15
14
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
  • 303
  • 2
  • 7
  • 20
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
  • 689
  • 1
  • 16
  • 39
13
votes
3 answers

Set interval between breaks on time axis

First let's create some example data. The times are stored using lubridate's hm as this seems the most suitable. library(tibble) library(lubridate) #> #> Attaching package: 'lubridate' #> The following object is masked from 'package:base': #> #> …
Greg
  • 487
  • 5
  • 15
13
votes
4 answers

Why are my functions on lubridate dates so slow?

I wrote this function which I use all the time: # Give the previous day, or Friday if the previous day is Saturday or Sunday. previous_business_date_if_weekend = function(my_date) { if (length(my_date) == 1) { if (weekdays(my_date) ==…
lebelinoz
  • 4,890
  • 10
  • 33
  • 56
13
votes
2 answers

Use lubridate to edit year within dplyr chain

I have a date frame similar to the following toy data: df <- structure(list(year = c(2014, 2014, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2016), date = structure(c(16229, 16236, 16243, 16250, 16257, 16600,…
MeetMrMet
  • 1,349
  • 8
  • 14
13
votes
1 answer

Subset dataframe based on POSIXct date and time greater than datetime using dplyr

I am not sure what is going wrong with selecting date times as a POSIXct format. I have read several comments on subsetting a dataframe based on as.Date and I can get that to work without an issue. I have also read many posts suggesting that…
Bill Perry
  • 463
  • 1
  • 5
  • 13
13
votes
2 answers

In R, use lubridate to convert hms objects into seconds

simple question in lubridate--I want to convert an hms object into its appropriate number of seconds since the start of the day. For instance library(lubridate) hms("12:34:45") then I want to know exactly how long 12 hours, 34 minutes, and 45…
tomw
  • 3,114
  • 4
  • 29
  • 51
12
votes
2 answers

Why does dmy() in the lubridate package not work with NAs? What is a good workaround?

I stumbled across a peculiar behavior in the lubridate package: dmy(NA) trows an error instead of just returning an NA. This causes me problems when I want to convert a column with some elements being NAs and some date-strings that are normally…
Christoph_J
  • 6,804
  • 8
  • 44
  • 58
12
votes
2 answers

How to get week starting date from a date in R

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]…
krish
  • 1,388
  • 2
  • 18
  • 28