将时区添加到data.table中的POSIXct对象 [英] Adding timezone to POSIXct object in data.table

查看:42
本文介绍了将时区添加到data.table中的POSIXct对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个data.table对象,其中日期和时间列存储为IDate/ITime对象.我也有一个时区列,其中时区以字符形式给出.

I have a data.table object with the columns date and time stored as IDate/ITime objects. I also have a time zone column where the time zone is given as character.

现在,我想创建使用POSIXct格式的列DateTime.但是我不知道如何为对象添加正确的时区.

Now I want to create a column DateTime that is using the POSIXct format. However I can't figure out how to add the correct time zone to the object.

#Create the data.table object
dtData <- data.table(
Index = seq(1,5), 
Time= as.ITime(c('16:00', '16:00', '12:30', '16:00', '15:00')),
Date = as.IDate(rep('2015-05-28', 5)),
TimeZone=c('America/New_York', 'America/New_York', 'Europe/London', 'Asia/Hong_Kong', 'Japan'))

#This gives an error of invalid tz value
dtData[, psxDateTime:=as.POSIXct(Date, time = Time, tz = TimeZone)]

#This seem to set every row to Japan time zone
dtData[, psxDateTime:=as.POSIXct(Date, time = Time, tz = TimeZone), by=Index]
print(dtData$psxDateTime)

有人能指出我正确的方向吗?谢谢.

Would anyone be able to point me in the right direction? Thanks.

在阅读了David Arenburg和akrun的评论之后,似乎无法将具有不同时区的POSIXct对象存储在一个data.table列中.甚至组合矢量也会更改时区属性:

After reading the comments by David Arenburg and akrun it looks like that there is no way to store POSIXct objects with different time zones in one data.table column. Even combining a vector changes the time zone attribute:

Date1 <- as.POSIXct('2015-05-28 16:00', tz='America/New_York')
Date2 <- as.POSIXct('2015-05-28 12:00', tz='Japan')
Date3 <- c(Date1, Date2)
print(Date1)
print(Date2)
print(Date3)

推荐答案

我正在关闭此问题,因为似乎不可能在data.table的一列中包含具有不同时区的POSIXct对象.

I am closing this question as it seems like it is not possible to have POSIXct objects with different time zones in one column of a data.table.

这篇关于将时区添加到data.table中的POSIXct对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆