Adding timezone to POSIXct object in data.table

笑着哭i 提交于 2019-12-24 03:46:11

问题


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.

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.

EDIT:

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)

回答1:


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.



来源:https://stackoverflow.com/questions/30506513/adding-timezone-to-posixct-object-in-data-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!