如何将lubridate as_datetime函数与dplyr mutate和case_when函数结合使用?

时间:2019-03-26 07:51:45

标签: r dplyr lubridate

我正在尝试操纵dttm变量,以根据数字id向量针对不同的时区进行调整。使用字符向量作为新值,我可以根据id操作变量而不会出现问题。但是,当我尝试使用date_time()函数创建新值时,每个值都会收到case_when中第一项的结果。

id向量是数字,我试图将类转换为因子和字符。这个问题有关。

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date

df1 <- tibble(
  id = c(1, 2, 3),
  date_time = rep(as_datetime("2018-01-01 12:34:56", tz = "Europe/Zurich"), 3)
) %>% 
  mutate(
    date_time2 = case_when(
      id == 1 ~ "one",
      id == 2 ~ "two",
      TRUE ~ "three"
    )
  )


df2 <- tibble(
  id = c(1, 2, 3),
  date_time = rep(as_datetime("2018-01-01 12:34:56", tz = "Europe/Zurich"), 3)
) %>% 
  mutate(
    date_time2 = case_when(
      id == 1 ~ as_datetime(date_time, tz = "America/New_York"),
      id == 2 ~ as_datetime(date_time, tz = "Asia/Kolkata"),
      TRUE ~ date_time
    )
  )

df3 <- tibble(
  id = c(1, 2, 3),
  date_time = rep(as_datetime("2018-01-01 12:34:56", tz = "Europe/Zurich"), 3)
) %>% 
  mutate(
    date_time2 = case_when(
      id == 1 ~ as_datetime(date_time, tz = "Asia/Kolkata"),
      id == 2 ~ as_datetime(date_time, tz = "America/New_York"),
      TRUE ~ date_time
    )
  )


df1 
#> # A tibble: 3 x 3
#>      id date_time           date_time2
#>   <dbl> <dttm>              <chr>     
#> 1     1 2018-01-01 12:34:56 one       
#> 2     2 2018-01-01 12:34:56 two       
#> 3     3 2018-01-01 12:34:56 three

df2
#> # A tibble: 3 x 3
#>      id date_time           date_time2         
#>   <dbl> <dttm>              <dttm>             
#> 1     1 2018-01-01 12:34:56 2018-01-01 06:34:56
#> 2     2 2018-01-01 12:34:56 2018-01-01 06:34:56
#> 3     3 2018-01-01 12:34:56 2018-01-01 06:34:56

df3
#> # A tibble: 3 x 3
#>      id date_time           date_time2         
#>   <dbl> <dttm>              <dttm>             
#> 1     1 2018-01-01 12:34:56 2018-01-01 17:04:56
#> 2     2 2018-01-01 12:34:56 2018-01-01 17:04:56
#> 3     3 2018-01-01 12:34:56 2018-01-01 17:04:56

reprex package(v0.2.1)于2019-03-26创建

df1显示了我的期望。

df2中,我希望date_time2 == 2的id显示“ 2018-01-01 17:04:56”而不是“ 2018-01-01 06: 34:56”。

df3中,我希望date_time2 == 3的id显示“ 2018-01-01 12:34:56”而不是“ 2018-01-01 17”: 04:56“。

2 个答案:

答案 0 :(得分:1)

这似乎是一个错误(可能是dpylr的错误,因为之前的日期存在问题)。

这是一个可行的解决方案(不要问我为什么可行:))

tibble(
  id = c(1, 2, 3),
  date_time = rep(as_datetime("2018-01-01 12:34:56", tz = "Europe/Zurich"), 3)
) %>% 
  mutate(
    date_time2 = case_when(
      id == 1 ~ as_datetime(as.character(as_datetime(date_time, tz = "America/New_York"))),
      id == 2 ~ as_datetime(as.character(as_datetime(date_time, tz = "Asia/Kolkata"))),
      TRUE ~  as_datetime(as.character(date_time))

    )
  )

# A tibble: 3 x 3
     id date_time           date_time2         
  <dbl> <dttm>              <dttm>             
1     1 2018-01-01 12:34:56 2018-01-01 06:34:56
2     2 2018-01-01 12:34:56 2018-01-01 17:04:56
3     3 2018-01-01 12:34:56 2018-01-01 12:34:56

答案 1 :(得分:1)

我们可以使用force_tzs包中的lubridate。我们可以为tzones参数提供不同的时区设置。在这种情况下,如果您知道时区的顺序,则不需要case_when

library(dplyr)
library(lubridate)

df2 %>%
  mutate(date_time2 = force_tzs(date_time, tzones = c("America/New_York", "Asia/Kolkata", "UTC")))
# # A tibble: 3 x 3
#      id date_time           date_time2         
#   <dbl> <dttm>              <dttm>             
# 1     1 2018-01-01 12:34:56 2018-01-01 17:34:56
# 2     2 2018-01-01 12:34:56 2018-01-01 07:04:56
# 3     3 2018-01-01 12:34:56 2018-01-01 12:34:56

df3 %>%
  mutate(date_time2 = force_tzs(date_time, tzones = c("Asia/Kolkata", "America/New_York", "UTC")))
# # A tibble: 3 x 3
#      id date_time           date_time2         
#   <dbl> <dttm>              <dttm>             
# 1     1 2018-01-01 12:34:56 2018-01-01 07:04:56
# 2     2 2018-01-01 12:34:56 2018-01-01 17:34:56
# 3     3 2018-01-01 12:34:56 2018-01-01 12:34:56