我正在使用 R 中的 POSIXct 数据类型。在我的工作中,我合并了一个函数,该函数在向量中返回两个 POSIXct 日期。但是,我发现了一些意想不到的行为。我写了一些示例代码来说明我的问题:
# POSIXct returning issue:
returnTime <- function(date) {
oneDay <- 60 * 60 * 24
nextDay <- date + oneDay
print(date)
print(nextDay)
return(c(date, nextDay))
}
myTime <- as.POSIXct("2015-01-01", tz = "UTC")
bothDays <- returnTime(myTime)
print(bothDays)
函数中的打印语句给出:
[1] "2015-01-01 UTC"
[1] "2015-01-02 UTC"
而代码末尾的打印语句给出:
[1] "2014-12-31 19:00:00 EST" "2015-01-01 19:00:00 EST"
我明白发生了什么,但我不明白为什么。这可能是一个让我难以理解的简单错误,但我真的很困惑。我不明白为什么返回时时区会发生变化。该类仍然是 POSIXct,只是时区发生了变化。
此外,我做了与上面相同的操作,但只是返回了其中一个日期并且日期的时区没有改变。我现在可以解决这个问题,但想看看是否有人对我的问题有任何见解。先感谢您!
感谢下面的帮助。我反而做了:
return(list(date, nextDay))
这解决了我的时区被删除的问题。
最佳答案
来自 ?c.POSIXct
:
Using
c
on "POSIXlt" objects converts them to the current time zone, and on "POSIXct" objects drops any "tzone" attributes (even if they are all marked with the same time zone).
另见 here .
关于r - R 中的奇数 POSIXct 函数行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32489458/