根据R中的状态列转换UTC时区

wlwcrazw  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(161)

我有一个日期列在UTC时区的数据框,我想创建另一个列,将我的UTC时区wrt转换为美国时区。
我有1000的行与美国不同的国家。
我的代理数据框如下所述

df<-structure(list(UTC_date = structure(list(sec = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), min = c(14L, 21L, 17L, 38L, 45L, 25L, 44L, 11L,09L, 27L), hour = c(3L, 0L, 16L, 16L, 17L, 8L, 17L, 1L, 2L, 4L), 
                                         mday = c(2L, 2L, 15L, 12L, 19L, 18L, 25L, 17L, 07L, 17L),
                                         mon = c(9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), 
                                         year = c(121L,121L, 121L, 121L, 121L, 121L, 121L, 121L, 121L, 121L), 
                                         wday = c(1L,4L, 1L, 1L, 1L, 4L, 1L, 3L, 3L, 3L), 
                                         yday = c(297L, 300L,297L, 297L, 297L, 300L, 297L, 299L, 299L, 299L), isdst = c(1L,1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
                                         zone = c("CDT", "CDT","CDT", "CDT", "CDT", "CDT", "CDT", "CDT", "CDT", "CDT"),
                                         
                                         gmtoff = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_,
                                                    
                                                    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
                                                    
                                                    NA_integer_)), class = c("POSIXlt", "POSIXt")), StoreState = c("TX","MS", "AZ", "SC", "WI", "MO", "TX", "TX", "MO", "TX")), 
             row.names = c(NA,10L), class = "data.frame")
str(df)
head(df)

我正在寻找的是下面的o/p作为第一行的例子

输入示例

UTC_Date                State                          
2021-10-02 03:14:00     TX

输出示例

UTC_Date                State                         Local Time as per State
2021-10-02 03:14:00     TX                              2021-10-02  21:14:00
6ie5vjzr

6ie5vjzr1#

您可以使用force_tzs函数转换为相应的时区(tz)。

library(lubridate)

 df %>% 
  mutate(tz = case_when(
  StoreState=="AZ"~ "US/Mountain",
  StoreState=="SC"~ "US/Eastern",
  TRUE ~ "US/Central"
  )) %>% 
  mutate('Local Time as per State'= force_tzs(UTC_date, tzones = tz)) %>% 
  select(UTC_date, State= StoreState, 'Local Time as per State')

相关问题