将时区的字符变量应用于在lubridate中解析的各个日期时间值

时间:2014-04-17 02:35:38

标签: r datetime timezone lubridate

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

我有一个数据框,其中包含本地日期时间和Olson时区的字符变量。在解析时,是否有可能让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专业版(64位)和lubridate v1.3.3上使用R v3.0.2 w RStudio v0.97.551。

1 个答案:

答案 0 :(得分: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")