转换到R中的本地时间 - 时区的向量 [英] Converting to Local Time in R - Vector of Timezones
问题描述
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
ortz = as.character(DS$Factor)
in any of the POSIXct/POSIXlt functions (includingformat()
andstrftime()
) 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/PacificThanks, 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屋!