1

我想使用 lubridate 将字符串解析为不同时区的日期时间。

我有一个数据框,其中包含本地日期时间和奥尔森时区的字符变量。是否可以让 lubridate 在解析时从数据框中获取每行数据的时区字符串?或者,在解析后强制时区也可以满足我的需求。

# Example data
df <- data.frame(fly = factor(c("AKL-SFO", "SFO-JFK")), 
                 dpt = c("2013-05-20 19:40:00", "2013-05-20 16:00:00"), 
                 dtz = c("Pacific/Auckland", "America/Los_Angeles"), 
                 stringsAsFactors = FALSE)

# Load required package
require(lubridate)

# try to set tz during parsing
df$dtdep <- ymd_hms(df$dpt) # parses to default UTC
df$dtdep2 <- ymd_hms(df$dpt, tz = "dtz") # parses to GMT and errors x4
# Warning messages:
# 1: In as.POSIXct.POSIXlt(lt) : unknown timezone 'dtz'
# 2: In as.POSIXlt.POSIXct(ct) : unknown timezone 'dtz'
# 3: In as.POSIXct.POSIXlt(t) : unknown timezone 'dtz'
# 4: In as.POSIXlt.POSIXct(ct) : unknown timezone 'dtz'
df$dtdep2 # returns[1] "2013-05-20 19:40:00 GMT" "2013-05-20 16:00:00 GMT"
# Warning message: In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'dtz'

df$dtdep3 <- ymd_hms(df$dpt, tz = paste(df$dtz)) 
# Warning messages:
# 1: In if (tz != "UTC") { :
#   the condition has length > 1 and only the first element will be used
# 2: In if (!is.na(new.tz)) attr(date, "tzone") <- new.tz :
#   the condition has length > 1 and only the first element will be used
# Error in as.POSIXlt.POSIXct(x, tz) : invalid 'tz' value
df$dtdep3 # Error in as.POSIXlt.POSIXct(x, tz) : invalid 'tz' value

在将所有数据解析为 UTC 日期时间后,我遵循了类似的路径尝试使用 force_tz() 更改 tz。

# try to change tz after parsing with force_tz()
df$ftz <- force_tz(df[1, "dtdep" ], tz = "Pacific/Auckland") # turns all into NZST of first row
df$ftz1 <- force_tz(df$dtdep, tz = "dtz") # gives same 4 errors as above and returns GMT 
df$ftz1 # Warning message: In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'dtz'
df$ftz2 <- force_tz(df$dtdep, tz = df$dtz) # turns all into NZST.
# Warning message: In if (!is.na(new.tz)) attr(date, "tzone") <- new.tz :   the condition has length > 1 and only the first element will be used
# df$ftz3 <- force_tz(df$dtdep, tz = paste(df$dtz)) is the same as ftz2.

也许使用 plyr 或 for 循环可以让我解析到不同的时区?我是 R 新手,经过 plyr 和 for 循环的反复试验,我无法让它工作。任何帮助将不胜感激。

我在 Windows 7 pro(64 位)和 lubridate v1.3.3 上使用 R v3.0.2 w RStudio v0.97.551。

4

1 回答 1

1

可以试试:

library(lubridate)
## list seems better at preserving POSIXct class
trial <- list()
for (i in 1:nrow(df))
  trial[[i]] <- ymd_hms(df$dpt[i], tz = df$dtz[i], locale = "en_US")
于 2014-08-22T22:52:47.970 回答