Using strptime for text strings with a "AM or PM" worked fine in R version 3.0.2
> strptime("8/25/2015 6:38:41 PM", "%m/%d/%Y %I:%M:%S %p")
[1] "2015-08-25 18:38:41"
I recently upgraded to R 3.2.2 and now find this commend returns a NA:
>strptime("8/25/2015 6:38:41 PM", "%m/%d/%Y %I:%M:%S %p")
[1] NA
It appears to be something to do with the "PM". If I remove the "PM" and use the command as follows, it works (but of course it interprets as AM not PM):
>strptime("8/25/2015 6:38:41", "%m/%d/%Y %H:%M:%S")
[1] "2015-08-25 06:38:41 NZST"
What am I missing here?
Update:
Thanks all for your comments:
I reinstalled R 3.2.2 but got the same error
> strptime("8/25/2015 6:38:41 PM", "%m/%d/%Y %I:%M:%S %p")
[1] NA
and here is the session info as requested
> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_New Zealand.1252 LC_CTYPE=English_New Zealand.1252
[3] LC_MONETARY=English_New Zealand.1252 LC_NUMERIC=C
[5] LC_TIME=English_New Zealand.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
Then I changed the locale as suggested in a related post
> Sys.setlocale(category="LC_TIME","C")
[1] "C"
> strptime("8/25/2015 6:38:41 PM", "%m/%d/%Y %I:%M:%S %p")
[1] "2015-08-25 18:38:41 NZST"
And it worked - but does this mean I have to change the locale each time I want to convert a text time into a POSIXct class?
Try lubridate from hadleyverse
library(lubridate)
arrive <- ymd_hms("2011-06-04 12:00:00", tz = "Pacific/Auckland")
arrive
## [1] "2011-06-04 12:00:00 NZST"
https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With