R: Why does strptime always return NA when I try to format a date string?
strptime
automatically runs as.character
on the first argument (so it doesn't matter that it's a factor) and any trailing characters not specified in format=
are ignored (so "EDT" doesn't matter).
The only issues are the typo @Ben Bolker identified (%m
should be %d
) and %H
should be %I
(?strptime
says you should not use %H
with %p
).
# %b and %m are both *month* formats
strptime("Jul 05, 2011 09:30 PM EDT", "%b %m, %Y %H:%M %p", tz="")
# [1] NA
# change %m to %d and we no longer get NA, but the time is wrong (AM, not PM)
strptime("Jul 05, 2011 09:30 PM EDT", "%b %d, %Y %H:%M %p", tz="")
# [1] "2011-07-05 09:30:00"
# use %I (not %H) with %p
strptime("Jul 05, 2011 09:30 PM EDT", "%b %d, %Y %I:%M %p", tz="")
# [1] "2011-07-05 21:30:00"
NA while using strptime function
I find another thread which gives me a solution. Because my system language is not English, it cannot recognize "October". After I change it to "10", the code runs perfectly. Thanks for all the help.
as.Date returning NA while converting from 'ddmmmyyyy'
Works for me. The reasons it doesn't for you probably has to do with your system locale.
?as.Date
has the following to say:
## This will give NA(s) in some locales; setting the C locale
## as in the commented lines will overcome this on most systems.
## lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")
x <- c("1jan1960", "2jan1960", "31mar1960", "30jul1960")
z <- as.Date(x, "%d%b%Y")
## Sys.setlocale("LC_TIME", lct)
Worth a try.
No way to solve as.Date returning NA
The format should match the order, %m
is for numeric months
as.Date(date, format="%d-%b-%Y")
#[1] "1994-11-23"
Also, this can be done with lubridate
library(lubridate)
dmy(date)
#[1] "1994-11-23"
Or with anytime
anytime::anydate(date)
#[1] "1994-11-23"
Changing date format (NA error)
You can use the correct format
df$date <- as.Date(df$date, format='%Y%m%d')
It is not clear whether you have numeric or non-numeric 'date' column. If it is 'numeric', convert to 'character' first
df$date <- as.Date(as.character(df$date), format='%Y%m%d')
But, strptime
would work even if the column is numeric
.
Or using library(lubridate)
library(lubridate)
ymd(df$date)
strptime not recognizing %b/%B?
I am a bit confused by R's behaviour in the case of missing values in strptime
. The documentation is clear enough:
For strptime the input string need not specify the date completely: it
is assumed that unspecified seconds, minutes or hours are zero, and an
unspecified year, month or day is the current one. Some components may
be returned as NA (but an unknown tzone component is represented by an
empty string).
But that doesn't always seem to be the case with months. Here is a particularly odd example:
strptime('Nov-01', '%B-%d')
[1] "2014-11-01"
strptime('2009-Nov', '%Y-%B')
[1] NA
An obvious workaround based on the above would be to make a simple function.
convert.months <- function(x)
strptime(paste(x,strftime(Sys.time(),'%d')), '%b %d')
Which is ugly, but it works:
convert.months(c("Nov","Dec"))
[1] "2014-11-18 EST" "2014-12-18 EST"
To be clear here are the test cases, which, according to my interpretation of the documentation, should all work:
strptime('11', '%m') # Fail
strptime('11-01', '%m-%d') # Works
strptime('2009-11', '%Y-%m') # Fail
strptime('Nov', '%b') # Fail
strptime('Nov-01', '%b-%d') # Works
strptime('2009-Nov', '%Y-%b') # Fail
Related Topics
Change Font Size for All Inline Equations R Markdown
R Find Overlap Among Time Periods
How to Make Hyperlinks in The Pop-Up of a Tm_Bubbles Item
Loop with a Defined Ggplot Function Over Multiple Dataframes
How to Extract Bold Text from a PDF Using R
Specific Spaces Between Bars in a Barplot - Ggplot2 - R
Fast Melted Data.Table Operations
How to Split a Dataframe Column by The First Instance of a Character in Its Values
Get Plot() Bounding Box Values
Is There an Efficient Way to Parallelize Mapply
How to Find The Indices Where There Are N Consecutive Zeroes in a Row
Split Character Vector into Sentences
How to Add Columnn Titles in a Sankey Chart Networkd3
Aws Dynamodb Support for "R" Programming Language