1

In this df I have latitute, longtitude and datetime and I want to calculate the timezone for that specific location for that specific time.

> dput(df)
structure(list(lat = structure(c(1L, 7L, 4L, 2L, 3L, 10L, 9L, 
6L, 5L, 8L), .Label = c("18<f8> 26' 01.91\" N", "18<f8> 29' 09.80\" N", 
"19<f8> 24' 36.35\" N", "19<f8> 40' 19.15\" N", "27<f8> 57' 02.07\" N", 
"32<f8> 22' 45.20\" N", "32<f8> 42' 56.65\" N", "33<f8> 29' 39.01\" N", 
"38<f8> 58' 56.02\" N", "41<f8> 39' 10.09\" N"), class = "factor"), 
    long = structure(c(1L, 10L, 3L, 2L, 4L, 6L, 8L, 7L, 5L, 9L
    ), .Label = c("068<f8> 57' 57.17\" W", "069<f8> 55' 52.36\" W", 
    "071<f8> 23' 28.83\" W", "071<f8> 26' 35.85\" W", "082<f8> 27' 25.83\" W", 
    "083<f8> 32' 16.32\" W", "086<f8> 18' 27.85\" W", "094<f8> 40' 14.85\" W", 
    "111<f8> 55' 33.78\" W", "117<f8> 09' 39.90\" W"), class = "factor"), 
    datetimeval = structure(c(503649060, 811578660, 812097060, 
    861690660, 762935460, 588666660, 681201060, 524817060, 673425060, 
    776500260), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("lat", 
"long", "datetimeval"), row.names = c(NA, -10L), class = "data.frame")
> 

I am using google_timezone function from googleway package. But I do not know how to convert the lat long to decimal format.

df$timezone <- googleway::google_timezone( c(-37.81659, 144.9841),timestamp = df$datetimeval, key = key)
Stupid_Intern
  • 3,382
  • 8
  • 37
  • 74
  • Try `df1 <- df %>% mutate(timezone = map(datetimeval, ~ google_timezone(c(-37.81659, 144.9841),timestamp = .x, key = key)))` – akrun Apr 07 '19 at 20:00
  • @akrun the lat long should be in decimal. – Stupid_Intern Apr 07 '19 at 20:01
  • How did you get this lat, long format? What would be the output for `"18 26' 01.91\" N"`? BTW, I can't test it on googleway as i don't have a billing account – akrun Apr 07 '19 at 20:02
  • You can do this with `df %>% mutate_at(vars(lat, long), list(~ str_extract_all(str_remove(., ""), "[0-9.]+") %>% map_dbl(~ as.numeric(.x) %*% c(1, 1/60, 1/3600))))` – akrun Apr 07 '19 at 20:22
  • @akrun WOW. Thanks I think it works but it should also take the sign into factor where if it's north N or East E then it's positive and vice versa. – Stupid_Intern Apr 07 '19 at 20:25
  • Yes, it is working fine `out <- df %>% mutate_at(vars(lat, long), list(~ str_extract_all(str_remove(., ""), "[0-9.]+") %>% map_dbl(~ as.numeric(.x) %*% c(1, 1/60, 1/3600)))) %>% mutate(timezone = pmap(., ~ google_timezone(c(..1, ..2), timestamp = ..3, key = key)))` – akrun Apr 07 '19 at 20:27

1 Answers1

2

Here is one option to convert to numeric and extract the time zone base don the TimeZone API from google

library(tidyverse)
library(googleway)
# get the signs of the columns 'lat/long' by checking if there is 'N' or 'E',
# change it to 1 or else -1
signs <- df %>% 
           transmute_at(vars(lat, long), 
                list(~ c(-1, 1)[(str_detect(., "[NE]") + 1)] ))

# mutate the 'lat/long' columns by extracting the numeric part
# get a dot product sum (degree + (min/60) + (sec/3600)
df1 <- df %>%
         mutate_at(vars(lat, long),
     list(~ str_extract_all(str_remove(., "<f.*>"), "[0-9.]+") %>% 
               map_dbl(~ as.numeric(.x) %*% c(1, 1/60, 1/3600))))
# change the sign of the columns by multiplying with signs
df1[1:2] <- df1[1:2] * signs
# create the timezone column by looping through each row with `pmap`, 
# connect to TimeZone API and extract the details
out <- df1 %>% 
              mutate(timezone = pmap(., ~ google_timezone(c(..1, ..2), 
                timestamp = ..3, key = "<key>")))

-output

out
#        lat       long         datetimeval                                                     timezone
#1  18.43386  -68.96588 1985-12-17 01:31:00 0, -14400, OK, America/Santo_Domingo, Atlantic Standard Time
#2  32.71574 -117.16108 1995-09-20 02:31:00 3600, -28800, OK, America/Los_Angeles, Pacific Daylight Time
#3  19.67199  -71.39134 1995-09-26 02:31:00 0, -14400, OK, America/Santo_Domingo, Atlantic Standard Time
#4  18.48606  -69.93121 1997-04-22 02:31:00 0, -14400, OK, America/Santo_Domingo, Atlantic Standard Time
#5  19.41010  -71.44329 1994-03-06 01:31:00 0, -14400, OK, America/Santo_Domingo, Atlantic Standard Time
#6  41.65280  -83.53787 1988-08-27 02:31:00    3600, -18000, OK, America/New_York, Eastern Daylight Time
#7  38.98223  -94.67079 1991-08-03 02:31:00     3600, -21600, OK, America/Chicago, Central Daylight Time
#8  32.37922  -86.30774 1986-08-19 02:31:00     3600, -21600, OK, America/Chicago, Central Daylight Time
#9  27.95058  -82.45718 1991-05-05 02:31:00    3600, -18000, OK, America/New_York, Eastern Daylight Time
#10 33.49417 -111.92605 1994-08-10 02:31:00       0, -25200, OK, America/Phoenix, Mountain Standard Time
akrun
  • 874,273
  • 37
  • 540
  • 662