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?