Skip to content

Instantly share code, notes, and snippets.

@erikgregorywebb
Created June 7, 2019 02:31
Show Gist options
  • Save erikgregorywebb/da708b540c8e68296b30ed0ab67ad09b to your computer and use it in GitHub Desktop.
Save erikgregorywebb/da708b540c8e68296b30ed0ab67ad09b to your computer and use it in GitHub Desktop.
library(googlesheets)
library(lubridate)
library(ggplot2)
library(stringr)
# authorize the package
gs_auth()
# import
ifttt = gs_title('IFTTT Location Tracker')
locations = ifttt %>% gs_read(ws = 'Sheet1')
# de-authorize the package
gs_deauth()
# tidy location name
home = 'MASKED-FOR-PRIVACY'
work = 'MASKED-FOR-PRIVACY
locations = locations %>%
mutate(site = ifelse(location == home, 'Home', ifelse(location == work, 'Work', 'Other'))) %>%
select(-location)
# format date/time
locations = locations %>%
mutate(`date/time` = str_replace(`date/time`, 'at ', '')) %>%
mutate(date_time = mdy_hm(`date/time`)) %>%
select(-`date/time`)
# calculate commute times
n1 = 1
n2 = 1
home_to_work_times = list()
work_to_home_times = list()
for (i in 1:nrow(locations)) {
# home to work
if ((locations$type[i] == 'exited' & locations$site[i] == 'Home') &
(locations$type[i+1] == 'entered' & locations$site[i+1] == 'Work')) {
home_to_work_time = difftime(locations$date_time[i+1], locations$date_time[i], units="hours")
home_to_work_times[[n1]] = c(locations$type[i], locations$site[i], locations$date_time[i],
locations$type[i+1], locations$site[i+1], locations$date_time[i+1],
'Home to Work', home_to_work_time)
n1 = n1 + 1
}
# work to home
if ((locations$type[i] == 'exited' & locations$site[i] == 'Work') &
(locations$type[i+1] == 'entered' & locations$site[i+1] == 'Home')) {
work_to_home_time = difftime(locations$date_time[i+1], locations$date_time[i], units="hours")
work_to_home_times[[n2]] = c(locations$type[i], locations$site[i], locations$date_time[i],
locations$type[i+1], locations$site[i+1], locations$date_time[i+1],
'Work to Home', work_to_home_time)
}
n2 = n2 + 1
}
# combine, define data types
commute = bind_rows(
do.call(rbind, home_to_work_times) %>% as_data_frame(),
do.call(rbind, work_to_home_times) %>% as_data_frame()
) %>% select(start_type = V1, start_place = V2, start_time = V3,
end_type = V4, end_place = V5, end_time = V6,
route = V7, hours = V8) %>%
mutate(hours = as.numeric(hours)) %>%
mutate(start_time = as.POSIXct(as.numeric(start_time) + 60*60*2, origin = '1970-01-01')) %>%
mutate(end_time = as.POSIXct(as.numeric(end_time) + 60*60*2, origin = '1970-01-01'))
# density plot
plot = commute %>%
select(Hours = hours, Route = route) %>%
filter(Hours < 2) %>%
ggplot(., aes(x = Hours, fill = Route)) +
geom_density(alpha = .55) +
theme_minimal() +
theme(legend.position = 'bottom') +
labs(y = 'Density', title = 'Distribution of Commute Times',
subtitle = 'April - May 2019')
# export
setwd("~/Documents/Python/ifttt")
png('density-plot.png', width = 8, height = 6, units = 'in', res = 600)
plot
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment