我正在尝试在Mac OS X Mavericks(10.9.4)上将字符串转换为R v3.1.1中的POSIXct。这在升级Mavericks然后R之前有效。我非常简单的代码现在发出警告,我不明白为什么: -
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'
我尝试使用以下内容定义时区但返回了NA: -
as.POSIXct("2014-05-24 12:45", "%Y-%m-%d %hh:%mm", tz="Europe/London")
[1] NA
不确定我在这里做错了什么。
提前感谢您的帮助。
答案 0 :(得分:2)
首先,如果您查看?strptime
文档,您会看到%h
是月份缩写,类似%m
是月份十进制数。对于小时和分钟,您应该使用%H
和%M
其次,如果你在控制台中输入as.POSIXct
,你会发现它的第二个参数是tz
而不是format
,因此你需要指定{{1}将format参数传递给它时。另外,你默认将它传递给format =
tz
因此解决方案将是
as.POSIXct
# function (x, tz = "", ...)
# UseMethod("as.POSIXct")
# <bytecode: 0x0000000008ee6000>
# <environment: namespace:base>