更改posixct对象中的时区
在R中玩数据和时区可能很棘手。 这是我的问题:我想在posixct
对象上更改时区
R) data <- data.frame(x=c(1,2),dateTime=as.POSIXct(c("2010-03-11 03:30:00.432","2010-03-15 03:30:00.432"),format="%Y-%m-%d %H:%M:%S",tz="America/Montreal"))
R) data
x dateTime
1 1 2010-03-11 03:30:00
2 2 2010-03-15 03:30:00
R) str(data)
'data.frame': 2 obs. of 2 variables:
$ x : num 1 2
$ dateTime: POSIXct, format: "2010-03-11 03:30:00" "2010-03-15 03:30:00"
,但是如果我想更改时区,我唯一发现的是:
R) data$dateTime2 = format(data$dateTime,tz="Europe/Paris")
R) str(data)
'data.frame': 2 obs. of 3 variables:
$ x : num 1 2
$ dateTime : POSIXct, format: "2010-03-11 03:30:00" "2010-03-15 03:30:00"
$ dateTime2: chr "2010-03-11 09:30:00" "2010-03-15 08:30:00"
或
R) data$dateTime2 = as.POSIXlt(data$dateTime,tz="Europe/Paris")
R) str(data)
'data.frame': 2 obs. of 3 variables:
$ x : num 1 2
$ dateTime : POSIXct, format: "2010-03-11 03:30:00" "2010-03-15 03:30:00"
$ dateTime2: POSIXlt, format: "2010-03-11 09:30:00" "2010-03-15 08:30:00"
上帝(或某人)知道为什么知道为什么它不适用于posixct
R) data$dateTime2 = as.POSIXct(data$dateTime,tz="Europe/Paris")
R) str(data)
'data.frame': 2 obs. of 3 variables:
$ x : num 1 2
$ dateTime : POSIXct, format: "2010-03-11 03:30:00" "2010-03-15 03:30:00"
$ dateTime2: POSIXct, format: "2010-03-11 03:30:00" "2010-03-15 03:30:00"
我是否需要转换为字符
并将其施放到posixct
?
Playing with dateTimes and timezone can be tricky in R.
Here is my question: I want to change the time-zone on a POSIXct
object
R) data <- data.frame(x=c(1,2),dateTime=as.POSIXct(c("2010-03-11 03:30:00.432","2010-03-15 03:30:00.432"),format="%Y-%m-%d %H:%M:%S",tz="America/Montreal"))
R) data
x dateTime
1 1 2010-03-11 03:30:00
2 2 2010-03-15 03:30:00
R) str(data)
'data.frame': 2 obs. of 2 variables:
$ x : num 1 2
$ dateTime: POSIXct, format: "2010-03-11 03:30:00" "2010-03-15 03:30:00"
But if I want to change the timezone, the only thing I found is:
R) data$dateTime2 = format(data$dateTime,tz="Europe/Paris")
R) str(data)
'data.frame': 2 obs. of 3 variables:
$ x : num 1 2
$ dateTime : POSIXct, format: "2010-03-11 03:30:00" "2010-03-15 03:30:00"
$ dateTime2: chr "2010-03-11 09:30:00" "2010-03-15 08:30:00"
Or
R) data$dateTime2 = as.POSIXlt(data$dateTime,tz="Europe/Paris")
R) str(data)
'data.frame': 2 obs. of 3 variables:
$ x : num 1 2
$ dateTime : POSIXct, format: "2010-03-11 03:30:00" "2010-03-15 03:30:00"
$ dateTime2: POSIXlt, format: "2010-03-11 09:30:00" "2010-03-15 08:30:00"
God (or somebody on SO) knows why it doesn't work with POSIXct
R) data$dateTime2 = as.POSIXct(data$dateTime,tz="Europe/Paris")
R) str(data)
'data.frame': 2 obs. of 3 variables:
$ x : num 1 2
$ dateTime : POSIXct, format: "2010-03-11 03:30:00" "2010-03-15 03:30:00"
$ dateTime2: POSIXct, format: "2010-03-11 03:30:00" "2010-03-15 03:30:00"
Do I need to convert to character
and cast back to POSIXct
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它与
posixct
无法使用,因为base :: as.posixct.default
简单地返回x
如果已经posixct
。您可以通过tzone
属性更改时区:It doesn't work with
POSIXct
becausebase::as.POSIXct.default
simply returnsx
if it's alreadyPOSIXct
. You can change the timezone via thetzone
attribute:在
lubridate
包中,有一个函数with_tz
可以更改时区属性(实际上是 Joshua 所描述的)。将时区从
UTC
更改为CET
In the
lubridate
package there is a functionwith_tz
which changes the timezone attribute (effectively what Joshua described).Change timezone from
UTC
toCET
截至 2021 年 7 月 23 日。
indexTZ
已弃用。tzone
很好用。tzone(data$dateTime) <- "欧洲/巴黎"
As of 7/23/2021.
indexTZ
is deprecated.tzone
is good to use.tzone(data$dateTime) <- "Europe/Paris"