0

In fact I know how to do this using a for loop, as was discussed in the following post.

Converting to Local Time in R - Vector of Timezones

However, if the list is too long, (500,000 records), the computation takes forever. Is there a better way to do this?

My data (df) is in character format. It looks like this:

       raw_date             timezoneID
2012-07-01 01:41:48 America/Montreal
2012-07-01 01:41:55 America/Edmonton
2012-07-01 08:26:07 America/Montreal
2012-07-01 09:26:08  America/Toronto
2012-07-01 10:29:05  America/Toronto
2012-07-01 10:39:20  America/Toronto

Here's what I do:

tmp_time <- data.frame(EST_date = as.POSIXct(df$raw_date,  tz="EST"), TZID = df$timezoneID)
tmp_time$TZID <- as.character(tmp_time$TZID)

local_time <- vector()
for (i in (1:nrow(tmp_time))){
    local_time[i] <- (format(tmp_time$EST_date[i], tz=tmp_time$TZID[i],usetz = TRUE, "%Y-%m-%d %H:%M:%S"))
}
local_time
# [1] "2012-07-01 02:41:48 EDT" "2012-07-01 00:41:55 MDT"
# [3] "2012-07-01 09:26:07 EDT" "2012-07-01 10:26:08 EDT"
# [5] "2012-07-01 11:29:05 EDT" "2012-07-01 11:39:20 EDT"

As I said, the above solution works but it is very slow and on 500,000 records my computer almost hangs. Any clues on how to speed this up?

Community
  • 1
  • 1
asiehh
  • 553
  • 12
  • 22
  • I don't know much about R (yet), but if you can adapt C++ code, you could try using a more specialized library, such as [cctz](https://github.com/google/cctz). It will probably be faster. – Matt Johnson-Pint Dec 18 '15 at 15:16

1 Answers1

2
library(dplyr)

tmp_time %>% group_by(TZID) %>%
  mutate(tm=format(EST_date, tz=unique(TZID),
                   usetz = TRUE, "%Y-%m-%d %H:%M:%S"))
# Source: local data frame [6 x 3]
# Groups: TZID [3]

#              EST_date             TZID                      tm
#                (time)            (chr)                   (chr)
# 1 2012-07-01 01:41:48 America/Montreal 2012-07-01 02:41:48 EDT
# 2 2012-07-01 01:41:55 America/Edmonton 2012-07-01 00:41:55 MDT
# 3 2012-07-01 08:26:07 America/Montreal 2012-07-01 09:26:07 EDT
# 4 2012-07-01 09:26:08  America/Toronto 2012-07-01 10:26:08 EDT
# 5 2012-07-01 10:29:05  America/Toronto 2012-07-01 11:29:05 EDT
# 6 2012-07-01 10:39:20  America/Toronto 2012-07-01 11:39:20 EDT
Ven Yao
  • 3,680
  • 2
  • 27
  • 42