Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Why does strptime always return NA when I try to format a date string?

Tags:

date

r

Here's some of my data, read in from a file names AttReport_all:

Registration.Date                 Join.Time                Leave.Time
1 Jul 05, 2011 09:30 PM EDT Jul 07, 2011 01:05 PM EDT Jul 07, 2011 01:53 PM EDT
2 Jul 05, 2011 10:20 AM EDT Jul 07, 2011 01:04 PM EDT Jul 07, 2011 01:53 PM EDT
3 Jul 04, 2011 02:41 PM EDT Jul 07, 2011 12:49 PM EDT Jul 07, 2011 01:53 PM EDT
4 Jul 04, 2011 11:38 PM EDT Jul 07, 2011 12:49 PM EDT Jul 07, 2011 01:54 PM EDT
5 Jul 05, 2011 11:41 AM EDT Jul 07, 2011 12:54 PM EDT Jul 07, 2011 01:54 PM EDT
6 Jul 07, 2011 11:08 AM EDT Jul 07, 2011 01:16 PM EDT Jul 07, 2011 01:53 PM EDT

If I do strptime(AttReport_all$Registration.Date, "%b %m, %Y %H:%M %p", tz="") I get an array of NAs where I'm expecting dates.

Sys.setlocale("LC_TIME", "C") returns "C"

typeof(AttReport_all$Registration.Date) returns "integer"

is.factor(AttReport_all$Registration.Date) returns TRUE.

What am I missing?

Here's version output, if it helps: platform i386-pc-mingw32
arch i386
os mingw32
system i386, mingw32
status
major 2
minor 13.0
year 2011
month 04
day 13
svn rev 55427
language R
version.string R version 2.13.0 (2011-04-13)

like image 285
William Gunn Avatar asked Jul 13 '11 20:07

William Gunn


People also ask

What does Strptime do in R?

strptime() function in R Language is used to parse the given representation of date and time with the given template.

How do I convert a character to a date column in R?

You can use the as. Date( ) function to convert character data to dates. The format is as. Date(x, "format"), where x is the character data and format gives the appropriate format.

What is the default date format in R language?

Dates in R display in a string format and can be output in various ways, the default being YYYY-MM-DD. The most commonly used symbols to format dates are given in the table below.

How do you set the date in R?

To create a Date object from a simple character string in R, you can use the as. Date() function. The character string has to obey a format that can be defined using a set of symbols (the examples correspond to 13 January, 1982): %Y : 4-digit year (1982)


1 Answers

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"
like image 161
Joshua Ulrich Avatar answered Nov 14 '22 22:11

Joshua Ulrich