0% found this document useful (0 votes)
63 views1 page

Working With Dates and Time in R

The document discusses parsing and working with dates, times, and datetimes in R. It covers automatically and manually parsing dates, times, and datetimes in multiple formats using packages like anytime and hms. It also covers defining and calculating with time intervals, periods, and durations. ISO 8601 for representing datetimes is also defined.

Uploaded by

Abd-AllahSaleh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views1 page

Working With Dates and Time in R

The document discusses parsing and working with dates, times, and datetimes in R. It covers automatically and manually parsing dates, times, and datetimes in multiple formats using packages like anytime and hms. It also covers defining and calculating with time intervals, periods, and durations. ISO 8601 for representing datetimes is also defined.

Uploaded by

Abd-AllahSaleh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

> Parsing dates, datetimes and times > Time intervals

Automatic parsing # Some points in time

start_of_time1 <- ymd("1970-01-01")

Working with Dates and Times in R


end_of_time1 <- ymd("2012-12-21")

# The following uses the anytime package

start_of_time2 <- ymd("2001-01-01")

# Automatically parse dates in multiple formats with anydate()


end_of_time2 <- ymd("2019-12-12")

anydate(c("Jun 16, 1915", "18 October 1919")) # "1915-06-16" "1919-10-18"

# Specify the interval between two datetimes with interval()

# Automatically parse datetimes in multiple formats with anytime()


intrvl1 <- interval(start_of_time1, end_of_time1) # 1970-01-01 UTC--2012-12-21 UTC

anytime(c("22 Nov 1963 13:30", "September 15 1901 02:15"), tz = "EST") # "1963-11-22 13:30:00
Learn R online at www.DataCamp.com EST" "1901-09-15 02:15:00 EST" # Determine the length of an interval in seconds with int_length()

int_length(intrvl1) # 1356048000

Manual parsing # Determine the overlap between two intervals with intersect()

intrvl2 <- interval(start_of_time2, end_of_time2)

# Parse dates in year, month, day format with ymd()


intersect(intrvl1, intrvl2) # 2001-01-01 UTC--2012-12-21 UTC
ymd("1759 09 22") # "1759-09-22"

> Definitions used in this cheat sheet # Parse dates in month, day, year format with mdy()

mdy("05-12-1820") # "1820-05-12"

Periods and durations


# Define a period in years

# Parse dates in day, month, year format with dmy()

Date: a day stored as the number of days since 1970-01-0 dmy("01/09/1835") # "1835-09-01"

years(2) # "2y 0m 0d 0H 0M 0S"

POSIXct: stores date and time in seconds with the number of seconds beginning at 1 January 197
hms: a simple class for storing durations or time-of-day values and displaying them in the hh:mm:ss forma # Parse datetimes in ISO format
# Define a duration in years

POSIXlt: stores date and time information as separate components including seconds, minutes, hours, days, et ymd_hms("1972-06-30 23:59:59") # "1972-06-30 23:59:59 UTC"

dyears(2) # "63115200s (~2 years)"

Interval: Intervals represent specific intervals of the timeline, bounded by start and end date-times
Period: Record the datetime ranges/time span in “human” times, Like years and month # Parse datetimes in a single format with fast_strptime()
# Intervals for a leap year and non-leap year

Duration: Record the datetime ranges / time span in second fast_strptime("January 1, 1924", "%B %d, %Y") # "1924-01-01 UTC"

leap <- interval("2020-01-01", "2021-01-01")

non_leap <- interval("2021-01-01", "2022-01-01")

Difftime: The difference between two datetime objects # Parsing datetimes in multiple specified formats with parse_date_time()

parse_date_time(c("Jun 16, 1915", "18 October 1919"),


# Convert an interval to a period with as.period()

c("%b %d, %Y", "%d %B %Y")) # Returns "1915-06-16 UTC" "1919-10-18 UTC" as.period(leap) # "1y 0m 0d 0H 0M 0S"

> ISO 8601 datetimes


as.period(non_leap) # "1y 0m 0d 0H 0M 0S"

Parsing times # Convert an interval to a duration with as.duration()

as.duration(leap) # "31622400s (~1 years)"

# The following uses the hms package

ISO 8601 specifies datetimes from the largest to the smallest unit of time. YYYY-MM-DD HH:MM:SS TZ
as.duration(non_leap) # "31536000s (~52.14 weeks)"
Some of the advantages of ISO 8601 are # Parse times without dates

hms(56,12,15) # Returns 15:12:56 Date arithmetic


It avoids ambiguities between MM/DD/YYYY and DD/MM/YYYY format
# Subtract a historical date from today

The 4-digit year representation mitigates overflow problems after the year 209

>
today() - ymd("2000-02-29") # Time difference of 8291 days

Using numeric month values (08 not AUG) makes it language independent, so dates makes sense throughout the world
R is optimized for this format, since it makes comparison and sorting easier.
Making dates and datetimes from components # Start with a day before a timezone change and add a period of one day

ymd("2022-11-06", tz = "America/New_York") + days(1) # "2022-11-07 EST"

# Make dates from components with make_date()

# Start with a day before a timezone change and add a duration of one day

> Loading packages


make_date(1777, 4, 30) # "1777-04-30"

ymd("2022-11-06", tz = "America/New_York") + ddays(1) # "2022-11-06 23:00:00 EST"


# Make datetimes from components with make_datetime()

make_datetime(1945, 6, 16, 05, 29, 21, tz = "US/Mountain") # "1945-06-16 05:29:21 MWT" Rounding dates
Except where noted, all functionality is found in the lubridate package. Some functionality is also found in
the anytime, hms, and readr packages.
Extracting components
# Round dates to the nearest time unit

round_date(ymd("2004-10-04"), "week")

# Load lubridate

library(lubridate)

# Round dates to the previous time unit

# Extract the year from a date or datetime with year()


floor_date(ymd("2004-10-04"), "week")

year("1923-12-11") # 1923

# Load the other packages

library(anytime)
# Round dates to the next time unit

# Extract the day of the year from a date or datetime with yday()

ceiling_date(ymd("2004-10-04"), "week")
library(hms)
yday("1900-10-14") # 287

library(readr)
# Extract the month or month name from a date or datetime with month()

month("1857-03-27", label = TRUE) # Mar

> Getting the current date


# Extract the day of the week from a date or datetime with wday()

wday("1890-02-17", label = TRUE) # Mon

# Get the current date with today()

> Time zones


today() # "2022-11-11"

# Get the current datetime including timezone with now()

now() # "2022-11-11 08:52:19 EST"

The functions below are available in base R. Learn R Online at


# Get the current timezone

www.DataCamp.com
> Reading datetime data from CSV Sys.timezone() # "Asia/Kuala_Lumpur"

# List all known timezones

OlsonNames() # "Africa/Abidjan" ... "Zulu"


# The following uses the readr package

# Read data from a CSV file with read_csv()


# Specify a datetime that has a location-based timezone

read_csv(filename,
ymd_hms("1915-04-25 12:00:00", tz = "Australia/Eucla") # "1915-04-25 12:00:00 +0845"

col_types = cols(

# Specify date column with col_date()


# Specify a datetime that has a UTC offset timezone

ymd_hms("1915-04-25 12:00:00 +08:45") 3 "1915-04-25 03:15:00 UTC"

date_col = col_date("%m/%d/%Y")

# Specify datetime column with col_datetime()


# Use a different timezone for a datetime with with_tz()

datetime_col = col_datetime("%m/%d/%Y %I:%M:%S %p")


with_tz(ymd_hms("1915-04-25 09:00:00", tz = "Asia/Kuala_Lumpur"), "America/Chicago") 

# Specify time column with col_time()
# Returns "1915-04-24 20:00:00 CDT"

time_col = col_time("%I:%M:%S %p")

)
# Override the timezone for a datetime with force_tz()

) force_tz(ymd_hms("1915-04-25 09:00:00", tz = "Asia/Kuala_Lumpur"), "America/Chicago") 



# Returns "1915-04-25 09:00:00 CDT"

You might also like