What are the standard unambiguous date formats for string-to-date conversion in R?
This is documented behavior. From ?as.Date
:
format: A character string. If not specified, it will try
'"%Y-%m-%d"' then '"%Y/%m/%d"' on the first non-'NA' element,
and give an error if neither works.
as.Date("01 Jan 2000")
yields an error because the format isn't one of the two listed above. as.Date("01/01/2000")
yields an incorrect answer because the date isn't in one of the two formats listed above.
I take "standard unambiguous" to mean "ISO-8601" (even though as.Date
isn't that strict, as "%m/%d/%Y" isn't ISO-8601).
If you receive this error, the solution is to specify the format your date (or datetimes) are in, using the formats described in the Details section in ?strptime
.
Make sure that the order of the conversion specification as well as any separators correspond exactly with the format of your input string. Also, be sure to use particular care if your data contain day/month names and/or abbreviations, as the conversion will depend on your locale (see the examples in ?strptime
and read ?LC_TIME
; see also strptime
, as.POSIXct
and as.Date
return unexpected NA
).
Error with the “standard unambiguous date” for string-to-date conversion in R
The ambiguity in your call to as.Date
is whether the day or month comes first. To resolve this, you may use the format
parameter of as.Date
:
complete_dataset_1 = complete_dataset
%>% mutate(age_at_enrollment = (
as.Date(start_date, format="%m/%d/%Y") -
as.Date(birth_date, format="%m/%d/%Y")) / 365.25)
A more precise way to calculate the diff in years, handling the leap year edge case, would be to use the lubridate
package:
library(lubridate)
complete_dataset_1 = complete_dataset
%>% mutate(age_at_enrollment = time_length(difftime(
as.Date(start_date, format="%m/%d/%Y"),
as.Date(birth_date, format="%m/%d/%Y")), "years")
as.Date showing character string in unambiguous format even when strptime specified R
Your data has to be a character. So use this workaround:
weather_dataset$DATE <- as.Date(as.character(weather_dataset$DATE),"%Y%m%d")
Another possibility is the library(lubridate):
weather_dataset$DATE <- lubridate::ymd(weather_dataset$DATE)
Convert to date format error: character string is not in a standard unambiguous format
Try below:
# example data
df1 <- data.frame(
issue_d1 = c("Dec,2011","Nov,2014","Apr,2015"),
issue_d2 = c("Nov,2015","Sep,2017","Apr,2018"))
library(zoo)
df1$Months <-
(as.yearmon(df1$issue_d2, "%b,%Y") -
as.yearmon(df1$issue_d1, "%b,%Y")) * 12
df1
# issue_d1 issue_d2 Months
# 1 Dec,2011 Nov,2015 47
# 2 Nov,2014 Sep,2017 34
# 3 Apr,2015 Apr,2018 36
Turn string 12/27/2020 00:00:00 into a date?
We can use format
in as.Date
from base R
as
as.Date(str1, "%m/%d/%Y")
#[1] "2020-12-27"
Or if we need the parse_date_time
, specify the format
as a string
parse_date_time('12/27/2020 00:00:00', 'mdy HMS')
#[1] "2020-12-27 UTC"
To convert to Date
class, wrap with as.Date
or as_date
as_date(parse_date_time('12/27/2020 00:00:00', 'mdy HMS'))
#[1] "2020-12-27"
Or
mdy_hms('12/27/2020 00:00:00')
#[1] "2020-12-27 UTC"
With the format
in tidyverse
, it needs to match the full format. Here, we have Hour:Minute:Seconds as well, so we need the _hms
Or this can be automatically picked up with anydate
library(anytime)
anydate('12/27/2020 00:00:00')
#[1] "2020-12-27"
Related Topics
How to Format a Number as Percentage in R
How to Flatten/Merge Overlapping Time Periods
Returning Multiple Objects in an R Function
Unordered Combinations of All Lengths
Quit and Restart a Clean R Session from Within R
Pass a Vector of Variables into Lm() Formula
Format Numbers With Million (M) and Billion (B) Suffixes
Plotting Grouped Bar Charts in R
Forcing Garbage Collection to Run in R With the Gc() Command
Incomplete Final Line' Warning When Trying to Read a .Csv File into R
Summarizing by Subgroup Percentage in R
Convert Data.Frame Column Format from Character to Factor
How to Convert Long to Wide Format With Counts
How to Divide Each Row of a Matrix by Elements of a Vector in R
Remove Columns With Zero Values from a Dataframe
Return Elements of List as Independent Objects in Global Environment