Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- test = tibble(time = c(as_datetime('2019-01-01 00:00:00'),
- as_datetime('2019-01-01 01:00:00'),
- as_datetime('2019-01-01 00:00:00'),
- as_datetime('2019-01-01 01:00:00')),
- tz = c('EST','EST','Asia/Hong_Kong','Asia/Hong_Kong'))
- test %>% group_by(tz) %>% mutate(localtime = with_tz(time, tz[1]))
- time tz localtime
- <dttm> <chr> <dttm>
- 1 2019-01-01 00:00:00 EST 2019-01-01 00:00:00
- 2 2019-01-01 01:00:00 EST 2019-01-01 01:00:00
- 3 2019-01-01 00:00:00 Asia/Hong_Kong 2019-01-01 00:00:00
- 4 2019-01-01 01:00:00 Asia/Hong_Kong 2019-01-01 01:00:00
- test %>% group_by(tz) %>% mutate(localtime = with_tz(time, !!tz[1]))
- time tz localtime
- <dttm> <chr> <dttm>
- 1 2019-01-01 00:00:00 EST 2018-12-31 19:00:00
- 2 2019-01-01 01:00:00 EST 2018-12-31 20:00:00
- 3 2019-01-01 00:00:00 Asia/Hong_Kong 2018-12-31 19:00:00
- 4 2019-01-01 01:00:00 Asia/Hong_Kong 2018-12-31 20:00:00
- test %>% rowwise %>% mutate(localtime = with_tz(time, tz))
- time tz localtime
- <dttm> <chr> <dttm>
- 1 2019-01-01 00:00:00 EST 2019-01-01 08:00:00
- 2 2019-01-01 01:00:00 EST 2019-01-01 09:00:00
- 3 2019-01-01 00:00:00 Asia/Hong_Kong 2019-01-01 08:00:00
- 4 2019-01-01 01:00:00 Asia/Hong_Kong 2019-01-01 09:00:00
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement