问题
I'm trying to convert a string to POSIXct in R v3.1.1 on Mac OS X Mavericks (10.9.4). This worked before upgrading Mavericks then R. My very simple code is now giving a warning and I don't understand why :-
as.POSIXct("2014-05-24 12:45", "%Y-%m-%d %hh:%mm")
[1] "2014-05-24 12:45:00 GMT"
Warning messages:
1: In strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
unknown timezone '%Y-%m-%d %hh:%mm'
2: In as.POSIXct.POSIXlt(x) : unknown timezone '%Y-%m-%d %hh:%mm'
3: In strptime(xx, f <- "%Y/%m/%d %H:%M:%OS", tz = tz) :
unknown timezone '%Y-%m-%d %hh:%mm'
4: In as.POSIXct.POSIXlt(x) : unknown timezone '%Y-%m-%d %hh:%mm'
5: In strptime(xx, f <- "%Y-%m-%d %H:%M", tz = tz) :
unknown timezone '%Y-%m-%d %hh:%mm'
6: In as.POSIXct.POSIXlt(x) : unknown timezone '%Y-%m-%d %hh:%mm'
7: In strptime(x, f, tz = tz) : unknown timezone '%Y-%m-%d %hh:%mm'
8: In as.POSIXct.POSIXlt(as.POSIXlt(x, tz, ...), tz, ...) :
unknown timezone '%Y-%m-%d %hh:%mm'
9: In as.POSIXlt.POSIXct(x, tz) : unknown timezone '%Y-%m-%d %hh:%mm'
I tried defining the timezone using the following but got an NA returned :-
as.POSIXct("2014-05-24 12:45", "%Y-%m-%d %hh:%mm", tz="Europe/London")
[1] NA
Not sure what I'm doing wrong here.
Thanks in advance for any help.
回答1:
First of all, If you''ll look into the ?strptime
documentation, you will see that %h
is for month abbreviation and similarly %m
is for month decimal number. For hours and minutes you should use %H
and %M
Second of all, if you'll type as.POSIXct
in console, you'll see that it's second parameter is tz
rather than format
, thus you need to specify format =
when passing a format argument to it. Other wise you are passing it to tz
by default
as.POSIXct
# function (x, tz = "", ...)
# UseMethod("as.POSIXct")
# <bytecode: 0x0000000008ee6000>
# <environment: namespace:base>
Thus the solution would be
as.POSIXct("2014-05-24 12:45", format = "%Y-%m-%d %H:%M")
## [1] "2014-05-24 12:45:00 IDT"
as.POSIXct("2014-05-24 12:45", "%Y-%m-%d %H:%M", tz = "Europe/London")
## [1] "2014-05-24 12:45:00 BST"
来源:https://stackoverflow.com/questions/25749040/as-posixct-produces-unknown-timezone-error-after-mavericks-and-r-upgrade-in-ma