When I try to parse a timestamp in the following format: "Thu Nov 8 15:41:45 2012", only NA
is returned.
I am using Mac OS X, R 2.15.2 and Rstudio 0.97.237. The language of my OS is Dutch: I presume this has something to do with it.
When I try strptime
, NA
is returned:
var <- "Thu Nov 8 15:41:45 2012"
strptime(var, "%a %b %d %H:%M:%S %Y")
# [1] NA
Neither does as.POSIXct
work:
as.POSIXct(var, "%a %b %d %H:%M:%S %Y")
# [1] NA
I also tried as.Date
on the string above but without %H:%M:%S
components:
as.Date("Thu Nov 8 2012", "%a %b %d %Y")
# [1] NA
Any ideas what I could be doing wrong?
I think it is exactly as you guessed,
strptime
fails to parse your date-time string because of your locales. Your string contains both abbreviated weekday (%a
) and abbreviated month name (%b
). These time specifications are described in?strptime
:The issue of
locales
is relevant also foras.POSIXct
,as.POSIXlt
andas.Date
.From
?as.POSIXct
:From
?as.Date
:Thus, if weekdays and month names in the string differ from those in the current locale,
strptime
,as.POSIXct
andas.Date
fail to parse the string correctly andNA
is returned.However, you may solve this issue by changing the
locales
:With my personal locale I can reproduce your error:
Was just messing around with same problem, and found this solution to be much cleaner because there is no need to change any of system settings manually, because there is a wrapper function doing this job in the
lubridate
package, and all you have to do is set the argumentlocale
: