如何使用 POSIX DateTime 维护时区并避免 R 中强制引入的 NA?
How to maintain time zone with POSIX DateTime and avoid NAs introduced by coercion in R?
我创建了一个时间序列数据框,我将使用它来合并其他时间序列数据。
dates2010 <- seq(as.POSIXct("2010-06-15 00:00:00", tz = "GMT"), as.POSIXct("2010-09-15 23:00:00", tz = "GMT"), by="hour") # make string of DateTimes for summer 2010
dates2011 <- seq(as.POSIXct("2011-06-15 00:00:00", tz = "GMT"), as.POSIXct("2011-09-15 23:00:00", tz = "GMT"), by="hour") # make string of DateTimes for summer 2011
dates <- c(dates2010, dates2011) # combine the dates from both years
sites <- c("a", "b", "c") # make string of all sites
datereps <- rep(dates, length(sites)) # repeat the date string the same number of times as there are sites
sitereps <- rep(sites, each = length(dates)) # repeat each site in the string the same number of times as there are dates
n <- data.frame(DateTime = datereps, SiteName = sitereps) # merge two strings with all dates and all sites
n <- n[order(n$SiteName, n$Date),] # re-order based on site, then date
如果我运行上面的代码,'dates2010'和'dates2011'是GMT格式:dates2010[1]
"2011-06-15 00:00:00 GMT" .
但是当我出于某种原因创建对象 'dates' 时,格式切换为 EST:dates[1]
“2010-06-14 19:00:00 美国东部时间
也许与POSIX类有关?
class(dates2010)
[1] "POSIXct" "POSIXt"
我试图 change the default time zone for R 到格林威治标准时间以避免时区切换问题。当我尝试对数据帧排序 'n' 并将其他数据帧合并到 'n'.
时,这会导致 NA 强制错误
n <- n[order(n$SiteName, n$Date),]
Warning message:
In xtfrm.POSIXct(x) : NAs introduced by coercion
关于如何保持时区不变并避免 NA 强制错误的任何想法?谢谢!
c()
掉落属性。因此,当您创建 dates
时,时区被删除并自动默认为当前语言环境。幸运的是,您可以使用 structure()
并在那里设置时区。
dates <- structure(c(dates2010, dates2011), tzone = "GMT")
head(dates)
# [1] "2010-06-15 00:00:00 GMT" "2010-06-15 01:00:00 GMT"
# [3] "2010-06-15 02:00:00 GMT" "2010-06-15 03:00:00 GMT"
# [5] "2010-06-15 04:00:00 GMT" "2010-06-15 05:00:00 GMT"
如果 dates
已经创建,您可以稍后 add/change tzone
属性。
attr(dates, "tzone") <- "GMT"
我创建了一个时间序列数据框,我将使用它来合并其他时间序列数据。
dates2010 <- seq(as.POSIXct("2010-06-15 00:00:00", tz = "GMT"), as.POSIXct("2010-09-15 23:00:00", tz = "GMT"), by="hour") # make string of DateTimes for summer 2010
dates2011 <- seq(as.POSIXct("2011-06-15 00:00:00", tz = "GMT"), as.POSIXct("2011-09-15 23:00:00", tz = "GMT"), by="hour") # make string of DateTimes for summer 2011
dates <- c(dates2010, dates2011) # combine the dates from both years
sites <- c("a", "b", "c") # make string of all sites
datereps <- rep(dates, length(sites)) # repeat the date string the same number of times as there are sites
sitereps <- rep(sites, each = length(dates)) # repeat each site in the string the same number of times as there are dates
n <- data.frame(DateTime = datereps, SiteName = sitereps) # merge two strings with all dates and all sites
n <- n[order(n$SiteName, n$Date),] # re-order based on site, then date
如果我运行上面的代码,'dates2010'和'dates2011'是GMT格式:dates2010[1]
"2011-06-15 00:00:00 GMT" .
但是当我出于某种原因创建对象 'dates' 时,格式切换为 EST:dates[1]
“2010-06-14 19:00:00 美国东部时间
也许与POSIX类有关?
class(dates2010)
[1] "POSIXct" "POSIXt"
我试图 change the default time zone for R 到格林威治标准时间以避免时区切换问题。当我尝试对数据帧排序 'n' 并将其他数据帧合并到 'n'.
时,这会导致 NA 强制错误n <- n[order(n$SiteName, n$Date),]
Warning message:
In xtfrm.POSIXct(x) : NAs introduced by coercion
关于如何保持时区不变并避免 NA 强制错误的任何想法?谢谢!
c()
掉落属性。因此,当您创建 dates
时,时区被删除并自动默认为当前语言环境。幸运的是,您可以使用 structure()
并在那里设置时区。
dates <- structure(c(dates2010, dates2011), tzone = "GMT")
head(dates)
# [1] "2010-06-15 00:00:00 GMT" "2010-06-15 01:00:00 GMT"
# [3] "2010-06-15 02:00:00 GMT" "2010-06-15 03:00:00 GMT"
# [5] "2010-06-15 04:00:00 GMT" "2010-06-15 05:00:00 GMT"
如果 dates
已经创建,您可以稍后 add/change tzone
属性。
attr(dates, "tzone") <- "GMT"