转换到R中的本地时间 - 时区的向量 [英] Converting to Local Time in R - Vector of Timezones

查看:251
本文介绍了转换到R中的本地时间 - 时区的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一套来自美国各地的数据,我正试图把每个主题转换成当地时间。我有每个事件UTC时间戳,并已将其转换为POSIXct格式,但每次我尝试包括向量 tz = DS $因子任何POSIXct / POSIXlt函数(包括 format())中的tz = as.character(DS $ Factor) strftime())我得到一个错误,说:


as.POSIXlt.POSIXct(x,如果我只是输入 tz ='US / Eastern',那么tz = tz = $ tz' 它工作正常,但当然不是我的所有值都来自该时区。



如何将时间戳记到当地时间对于每个主题?
$ b $ DS $因子有5个值:US / Arizona US / Central US /美国东部/美国/太平洋



感谢,
速记

解决方案

  require(lubridate)
require(dplyr)

df = data.frame(timestring = c(2015-12-12 13:34:56,2015-12-14 16:23:32 ),
localzone = c(America / Los_Angeles,America / New_York),stringsAsFactors = F)

df $ moment = as.POSIXct(df $ timestring,format = %y%m-%d%H:%M:%S,tz =UTC)

df = df%>%rowwise()%>%mutate(localtime = force_tz(moment,localzone))

df


I have a set of data from across the US that I am trying to convert into local time for each "subject". I have UTC timestamps on each event and have converted those into POSIXct format, but every time I try to include a vector of tz = DS$Factor or tz = as.character(DS$Factor) in any of the POSIXct/POSIXlt functions (including format() and strftime()) I get an error that says:

Error in as.POSIXlt.POSIXct(x, tz = tz) : invalid 'tz' value

If I just enter tz = 'US/Eastern' it works fine, but of course not all of my values are from that time zone.

How do I get the time stamps into local time for each "subject"?

The DS$Factor has 5 values: US/Arizona US/Central US/Eastern US/Mountain US/Pacific

Thanks, Shorthand

解决方案

Bringing in dplyr and lubridate, I wound up doing something like:

require(lubridate)
require(dplyr)

df = data.frame(timestring = c("2015-12-12 13:34:56", "2015-12-14 16:23:32"),
                localzone = c("America/Los_Angeles", "America/New_York"), stringsAsFactors = F)

df$moment = as.POSIXct(df$timestring, format="%Y-%m-%d %H:%M:%S", tz="UTC")

df = df %>% rowwise() %>% mutate(localtime = force_tz(moment, localzone))

df

这篇关于转换到R中的本地时间 - 时区的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆