with_tz与时区的向量 [英] with_tz with a vector of timezones
问题描述
我有一个这样的数据框:
library(dplyr)
pre>
data< - data_frame $ b timestamp_utc = c('2015-11-18 03:55:04','2015-11-18 03:55:08',
'2015-11-18 03:55:10'),
local_tz = c('America / New_York','America / Los_Angeles',
'America / Indiana / Indianapolis')
)
我需要创建一个新的变量,将UTC时间戳转换为本地时间,如
local_tz
柱。但是,格式
和with_tz
(来自lubridate
)期望只有一个时区,而不是时区的向量。我正在寻找这样的东西:mutate(data,timestamp_local = with_tz(timestamp_utc,tzone = local_tz))
任何想法?
解决方案这是一种方法。结果必须是一个字符串,否则
unlist()
或c()
将会将结果转回到列表中的每个元素的系统时区。
它仍然很慢,因为它没有向量化。
> get_local_time< - function(timestamp_utc,local_tz){
l < - lapply(seq(length(timestamp_utc)),
function(x){format(with_tz(timestamp_utc [x],local_tz [x] ),%FT%T%z)})
unlist(l)
}
> mutate(data,timestamp_local = get_local_time(timestamp_utc,tzone = local_tz))
源:本地数据帧[3 x 3]
timestamp_utc local_tz timestamp_local
)(chr)(chr)
1 2015-11-18 03:55:04美国/纽约2015-11-17T22:55:04-0500
2 2015-11-18 03:55: 08美国/ Los_Angeles 2015-11-17T19:55:08-0800
3 2015-11-18 03:55:10美国/印第安纳州/印第安纳波利斯2015-11-17T22:55:10-0500
更新2015-11-24
使用
dplyr :: combine()
而不是unlist()
允许变量保持正确的数据时间时区属性,而不是转换为字符串。> get_local_time< - function(timestamp_utc,local_tz){
l < - lapply(seq(length(timestamp_utc)),
函数(x){with_tz(timestamp_utc [x],local_tz [x])} )
combine(l)
}
> mutate(data,timestamp_local = get_local_time(timestamp_utc,tzone = local_tz))
源:本地数据帧[3 x 3]
timestamp_utc local_tz timestamp_local
)(chr)(时间)
1 2015-11-18 03:55:04美国/纽约2015-11-17T22:55:04
2 2015-11-18 03:55:08美国/ Los_Angeles 2015-11-17T19:55:08
3 2015-11-18 03:55:10美洲/印第安纳州/印第安纳波利斯2015-11-17T22:55:10
I have a dataframe like so:
library(dplyr) data <- data_frame( timestamp_utc = c('2015-11-18 03:55:04', '2015-11-18 03:55:08', '2015-11-18 03:55:10'), local_tz = c('America/New_York', 'America/Los_Angeles', 'America/Indiana/Indianapolis') )
I need to create a new variable that converts the UTC timestamp to the local time as defined in the
local_tz
column. However, bothformat
andwith_tz
(fromlubridate
) expect only one timezone, not a vector of timezones. I'm looking for something like this:mutate(data, timestamp_local = with_tz(timestamp_utc, tzone = local_tz))
Any ideas?
解决方案Here is one method. With this, the result has to be a string, otherwise
unlist()
orc()
will turn the result back to the system timezone for every element in the list.It's still slow though because it is not vectorized.
> get_local_time <- function(timestamp_utc, local_tz) { l <- lapply(seq(length(timestamp_utc)), function(x) {format(with_tz(timestamp_utc[x], local_tz[x]), "%FT%T%z")}) unlist(l) } > mutate(data, timestamp_local = get_local_time(timestamp_utc, tzone = local_tz)) Source: local data frame [3 x 3] timestamp_utc local_tz timestamp_local (time) (chr) (chr) 1 2015-11-18 03:55:04 America/New_York 2015-11-17T22:55:04-0500 2 2015-11-18 03:55:08 America/Los_Angeles 2015-11-17T19:55:08-0800 3 2015-11-18 03:55:10 America/Indiana/Indianapolis 2015-11-17T22:55:10-0500
Update 2015-11-24
Using
dplyr::combine()
rather thanunlist()
allows the variable to remain datetimes with the right timezone attributes rather than converting to strings.> get_local_time <- function(timestamp_utc, local_tz) { l <- lapply(seq(length(timestamp_utc)), function(x) {with_tz(timestamp_utc[x], local_tz[x])}) combine(l) } > mutate(data, timestamp_local = get_local_time(timestamp_utc, tzone = local_tz)) Source: local data frame [3 x 3] timestamp_utc local_tz timestamp_local (time) (chr) (time) 1 2015-11-18 03:55:04 America/New_York 2015-11-17T22:55:04 2 2015-11-18 03:55:08 America/Los_Angeles 2015-11-17T19:55:08 3 2015-11-18 03:55:10 America/Indiana/Indianapolis 2015-11-17T22:55:10
这篇关于with_tz与时区的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文