with_tz与时区的向量 [英] with_tz with a vector of timezones

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

问题描述

我有一个这样的数据框:

  library(dplyr)
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')

pre>

我需要创建一个新的变量,将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, both format and with_tz (from lubridate) 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() or c() 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 than unlist() 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屋!

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