I have a data frame in R with the following structure:
df <- data.frame(
long = c(-112, 34, 39),
lat = c(41, -92, -76),
state=c("utah", "arkansas", "maryland"),
utc_time = c(1593855055, 1593826821, 1593826447)
)
long lat state utc_time
1 -112 41 utah 1593855055
2 34 -92 arkansas 1593826821
3 39 -76 maryland 1593826447
I would like to convert utc_time to local time using the lat,long or the state code. I have no idea what time the original utc stamp is from, but it seems likely GMT.
Thoughts? Thank you.
Update:
Thank you MrFlick. Yes, that was helpful. While I could not get the timezone package to work. I did use the accompanying shape file and did a spatial join.
TZ <- st_read("shape file from http://efele.net/maps/tz/us/")
df <- st_as_sf(df, coords = c("long", "lat"), crs = st_crs(TZ))
df <- st_join(df, TZ)
Now I have a dataframe that looks like this:
df <- data.frame(
TZID = c("America/New_York", "America/Denver", "America/New_York"),
utc_time = c(1593855055, 1593826821, 1593826447)
)
I'm still struggling to get R to recognize it as local time:
df$date <- as.POSIXct(df$utc_time, tz=TZID)
I don't know if I'm vectorizing incorrectly, or if I misunderstand the POSIXct command. Never the less, I found your answer amazingly helpful. Thank you.