Sometimes when importing files without metadata or if your file contains a date in a specific format, the variable will be processed as a Text or Nominal (categorical) variable instead of a Date/Time variable. You may want to convert them into a Date/Time variable to ensure the ordering is sequential and allow for previous period statistical testing. The Displayr team created an R package of functions called flipTime
that you can use to streamline the code needed to create your Date/Time variable using R. Converting dates entered as strings into numeric dates in R is relatively simple for a single string and for a vector of strings if you have consistent date information. It is far trickier if the date information is represented inconsistently but hopefully it will be made easier using the flipTime
package.
Requirements
- A Displayr document.
- A Text variable with dates in a consistent format, without missing data (if you have missing data you can use an Alternative function in the Technical Notes below).
- You should be aware of the other options for creating Date/Time variables in How to Create Date Variables in Displayr in case a different method would be easier for converting your date.
Method
The AsDate()
and AsDateTime()
functions are very flexible with respect to what they permit as characters to separate the components of the date strings. Warnings will be shown to the user when the dates are not in an unambiguous format.
library(flipTime)
AsDate("Jan. 10, 2016")
## [1] "2016-01-10"
AsDate("Jan/10 - 2016")
## [1] "2016-01-10"
However, they are also careful to not convert strings to dates that are clearly not dates:
AsDate("Jan 128")
## Error in AsDate("Jan 128"): Could not parse "Jan 128" into a valid
## date in any format.
AsDate("3.122")
## Error in AsDate("3.122"): Could not parse "3.122" into a valid date in
## any format.
The above example also demonstrates the default behavior of the functions to throw an error. This occurs when the date strings cannot be interpreted as dates. Both functions have an argument on.parse.failure
, which is used to control this behavior.
AsDate("foo", on.parse.failure = "warn")
## Warning in AsDate("foo", on.parse.failure = "warn"): Could not parse
## "foo" into a valid date in any format.
## [1] NA
AsDateTime("foo", on.parse.failure = "silent")
## [1] NA
Both functions provide an argument us.format
, to allow the user to specify whether the date strings are in a U.S. or international format. U.S. format is with the month coming before the day, such as Jan. 2,
1988
. By contrast, international format, has the day before the month, such as 21-10-1999
. The default behavior is to check both formats. In this case, if the format is ambiguous, the date strings will be converted according to the U.S. format. The user will also receive a warning.
AsDateTime("9/10/2010 10:20PM")
## Warning: Date formats are ambiguous, US format has been used.
## [1] "2010-09-10 22:20:00 UTC"
AsDateTime("9/10/2010 10:20PM", us.format = FALSE)
## [1] "2010-10-09 22:20:00 UTC"
The function AsDate()
is also able to interpret date intervals or periods, which can be useful when working with aggregated data. If the function encounters date periods, it will convert the start of the period to a date and return it.
AsDate("10/20/2015-12/02/2016")
## [1] "2015-10-20"
AsDate("may 2017-september 2017")
## [1] "2017-05-01"
AsDate("Dec/Apr 16")
## [1] "2015-12-01"
Technical Notes
The functions from the flipTime
package AsDate()
and AsDateTime()
build on the convenience and speed of the popular lubridate
function. AsDate()
and AsDateTime()
are careful to not convert any strings to dates when they are not formatted as dates. Additionally, they will also warn the user when the dates are not in an unambiguous format.
The package flipTime
provides utilities for working with time series and date-time data. The package can be installed from GitHub
using:
require(devtools)
install_github("Displayr/flipTime")
Alternative function
If your date format is not being picked up by the functions in the flipTime package, or you have any missing data values. Another function that you can use in R is the strptime()
function and provide the specific format. For example, for a date variable named datevar that has a format January 2, 2024 your code would be:
strptime(datevar, format = "%B %d, %Y")
you can find how to specify the format of the date based on the documentation here.
Next
How to Use R Code to Show the Date and Time
How to Convert Dates into Different Time Zones in Displayr
How to Work with R in Displayr