使用`unnest()`时区的奇怪行为

时间:2018-03-21 08:12:47

标签: r timezone tidyr lubridate unnest

unnest()关于时区的行为对我来说似乎很奇怪。

library(tidyverse)
library(lubridate)

Sys.getenv("TZ")
#[1] "Europe/Paris"

reprex_unnest <- tribble(
  ~datetime, ~locale,
  "2018-03-08 01:00:00",   "America/Argentina/Buenos_Aires",
  "2018-03-08 01:00:00",   "Europe/Kiev",
  "2018-03-08 01:00:00",   "UTC"
) %>%
  mutate(datetime = ymd_hms(datetime, tz = "UTC")
         , datetime_locale = map2(datetime, locale, ~ with_tz(.x, tzone = .y) ))%>%
  unnest(datetime_locale) %>% 
  mutate(tz_datetime = tz(datetime)
         , tz_datetime_locale = tz(datetime_locale))

reprex_unnest

# Source: local data frame [3 x 3]
# Groups: <by row>
# 
# # A tibble: 3 x 3
#   datetime            locale        datetime_locale     tz_datetime tz_datetime_locale
#   <dttm>              <chr>         <dttm>              <chr>       <chr>            
# 1 2018-03-08 01:00:00 America/Arge… 2018-03-08 01:00:00 UTC         UTC
# 2 2018-03-08 01:00:00 Europe/Kiev   2018-03-08 01:00:00 UTC         UTC
# 3 2018-03-08 01:00:00 UTC           2018-03-08 01:00:00 UTC         UTC

似乎with_tz未应用

reprex_no_unnest <- tribble(
  ~datetime, ~locale,
  "2018-03-08 01:00:00",   "America/Argentina/Buenos_Aires",
  "2018-03-08 01:00:00",   "Europe/Kiev",
  "2018-03-08 01:00:00",   "UTC"
) %>%
  mutate(datetime = ymd_hms(datetime, tz = "UTC")) %>% 
  mutate(datetime_locale = map2(datetime, locale, ~ with_tz(.x, tzone = .y) ))

str(reprex_no_unnest)

# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of  3 variables:
#  $ datetime       : POSIXct, format: "2018-03-08 01:00:00" ...
#  $ locale         : chr  "America/Argentina/Buenos_Aires" "Europe/Kiev" "UTC"
#  $ datetime_locale:List of 3
#   ..$ : POSIXct, format: "2018-03-07 22:00:00"
#   ..$ : POSIXct, format: "2018-03-08 03:00:00"
#   ..$ : POSIXct, format: "2018-03-08 01:00:00"

但在幕后,实际应用with_tz并返回预期结果。

我想这个问题归结为这个

tz(reprex$datetime_locale[1])
#[1] "UTC"
tz(reprex$datetime_locale[[1]])
#[1] "America/Argentina/Buenos_Aires"
tz(reprex$datetime_locale[[2]])
#[1] "Europe/Kiev"

列表列的tz实际上是UTC,我猜它优先于每个元素的tz

这是预期的R行为还是我错过了什么? 我该怎么做才能获得预期的结果?

0 个答案:

没有答案