lubridate R Package Documentation

repository·main·Indexed 21 days ago

https://github.com/tidyverse/lubridate

An R package for intuitive and robust date-time data management. It provides tools for parsing date-times via functions like ymd() and mdy(), extracting components using year() and month(), managing time zones with with_tz() and force_tz(), and performing mathematical operations using durations, periods, and intervals.

Tokens
771
Snippets
4
Records
5
Agent score
24%

What's inside lubridate

  1. Understand lubridate time span classes

    main

    Lubridate introduces three time span classes to expand mathematical operations on date-times:

    • durations: Measures the exact amount of time between two points.
    • periods: Tracks clock times accurately, accounting for leap years, leap seconds, and daylight savings time.
    • intervals: A summary of the time information between two points.
  2. Install lubridate

    main

    You can install lubridate via CRAN as part of the tidyverse collection, as a standalone package, or directly from GitHub using devtools.

    # The easiest way to get lubridate is to install the whole tidyverse:
    install.packages("tidyverse")
    
    # Alternatively, install just lubridate:
    install.packages("lubridate")
    
    # Or the development version from GitHub:
    # install.packages("devtools")
    devtools::install_github("tidyverse/lubridate")
  3. Parse date-times with intuitive functions

    main

    Lubridate provides fast and easy parsing functions that follow the order of date components (e.g., ymd(), dmy(), mdy()). These functions can also include time components like _hms (e.g., ymd_hms()).

    ymd(20101215)
    #> [1] "2010-12-15"
    
    mdy("4/1/17")
    #> [1] "2017-04-01"
  4. Get and set date-time components

    main

    Use functions like year(), month(), mday(), hour(), minute(), and second() to extract specific parts of a date-time. Many of these functions also support assignment to modify the date-time object. Functions like wday() can return labeled weekdays.

    bday <- dmy("14/10/1979")
    month(bday)
    #> [1] 10
    
    wday(bday, label = TRUE)
    #> [1] Sun
    #> Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
    
    year(bday) <- 2016
    wday(bday, label = TRUE)
    #> [1] Fri
    #> Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
  5. Handle time zones with with_tz() and force_tz()

    main

    Lubridate provides helper functions to manage time zone transitions:

    • with_tz(): Changes how the date-time is printed (converts the time to a new time zone while keeping the same instant in time).
    • force_tz(): Changes the actual time (re-assigns the time zone to the existing clock time without changing the hours/minutes).
    time <- ymd_hms("2010-12-13 15:30:30")
    time
    #> [1] "2010-12-13 15:30:30 UTC"
    
    # Changes printing
    with_tz(time, "America/Chicago")
    #> [1] "2010-12-13 09:30:30 CST"
    
    # Changes time
    force_tz(time, "America/Chicago")
    #> [1] "2010-12-13 15:30:30 CST"