Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rides by time of day: weekday vs. weekend #3

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added img/scooter-rides-weekday-vs-weekend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions scooter-rides-weekend-vs-weekday/.Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source("renv/activate.R")
3 changes: 3 additions & 0 deletions scooter-rides-weekend-vs-weekday/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
renv/library

.Rhistory
25 changes: 25 additions & 0 deletions scooter-rides-weekend-vs-weekday/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Scooter Report Card
The goal of this analysis was to compare weekday and weekend scooter traffic.

## File Structure
The [Rlang] script is both a place to run / develop code and a report of the results.

The renv.lock file controls the Rlang dependencies (via [renv]).
This helps you ensure you have everything you need to run the code.

The other files are artifacts that derive from the script.

## Usage
The first time, you may need to install the Rlang dependencies with:
```
renv::init()
```
This will only work if you have [renv] installed.

To start the script, run:
```
Rscript scooter-report-card.ipynb
```

[Rlang]: https://Rlang.org/index.html
[renv]: https://rstudio.github.io/renv/index.html
25 changes: 25 additions & 0 deletions scooter-rides-weekend-vs-weekday/renv.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"R": {
"Version": "3.6.1",
"Repositories": [
{
"Name": "CRAN",
"URL": "https://mirrors.nics.utk.edu/cran"
}
]
},
"Packages": {
"renv": {
"Package": "renv",
"Version": "0.7.0-126",
"Source": "GitHub",
"RemoteType": "github",
"RemoteHost": "api.github.com",
"RemoteRepo": "renv",
"RemoteUsername": "rstudio",
"RemoteRef": "master",
"RemoteSha": "b39688992a45e12f241c18f85f8b8432315e7027",
"Hash": "7f385af973e49e849f15e6d822a07511"
}
}
}
157 changes: 157 additions & 0 deletions scooter-rides-weekend-vs-weekday/renv/activate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@

local({

# the requested version of renv
version <- "0.7.0-126"

# avoid recursion
if (!is.na(Sys.getenv("RENV_R_INITIALIZING", unset = NA)))
return(invisible(TRUE))

# signal that we're loading renv during R startup
Sys.setenv("RENV_R_INITIALIZING" = "true")
on.exit(Sys.unsetenv("RENV_R_INITIALIZING"), add = TRUE)

# signal that we've consented to use renv
options(renv.consent = TRUE)

# load the 'utils' package eagerly -- this ensures that renv shims, which
# mask 'utils' packages, will come first on the search path
library(utils, lib.loc = .Library)

# check to see if renv has already been loaded
if ("renv" %in% loadedNamespaces()) {

# if renv has already been loaded, and it's the requested version of renv,
# nothing to do
spec <- .getNamespaceInfo(.getNamespace("renv"), "spec")
if (identical(spec[["version"]], version))
return(invisible(TRUE))

# otherwise, unload and attempt to load the correct version of renv
unloadNamespace("renv")

}

# construct path to renv in library
libpath <- local({

root <- Sys.getenv("RENV_PATHS_LIBRARY", unset = "renv/library")
prefix <- paste("R", getRversion()[1, 1:2], sep = "-")

# include SVN revision for development versions of R
# (to avoid sharing platform-specific artefacts with released versions of R)
devel <-
identical(R.version[["status"]], "Under development (unstable)") ||
identical(R.version[["nickname"]], "Unsuffered Consequences")

if (devel)
prefix <- paste(prefix, R.version[["svn rev"]], sep = "-r")

file.path(root, prefix, R.version$platform)

})

# try to load renv from the project library
if (requireNamespace("renv", lib.loc = libpath, quietly = TRUE))
return(renv::load())

# failed to find renv locally; we'll try to install from GitHub.
# first, set up download options as appropriate (try to use GITHUB_PAT)
install_renv <- function() {

message("Failed to find installation of renv -- attempting to bootstrap...")

# ensure .Rprofile doesn't get executed
rpu <- Sys.getenv("R_PROFILE_USER", unset = NA)
Sys.setenv(R_PROFILE_USER = "<NA>")
on.exit({
if (is.na(rpu))
Sys.unsetenv("R_PROFILE_USER")
else
Sys.setenv(R_PROFILE_USER = rpu)
}, add = TRUE)

# prepare download options
pat <- Sys.getenv("GITHUB_PAT")
if (nzchar(Sys.which("curl")) && nzchar(pat)) {
fmt <- "--location --fail --header \"Authorization: token %s\""
extra <- sprintf(fmt, pat)
saved <- options("download.file.method", "download.file.extra")
options(download.file.method = "curl", download.file.extra = extra)
on.exit(do.call(base::options, saved), add = TRUE)
} else if (nzchar(Sys.which("wget")) && nzchar(pat)) {
fmt <- "--header=\"Authorization: token %s\""
extra <- sprintf(fmt, pat)
saved <- options("download.file.method", "download.file.extra")
options(download.file.method = "wget", download.file.extra = extra)
on.exit(do.call(base::options, saved), add = TRUE)
}

# fix up repos
repos <- getOption("repos")
on.exit(options(repos = repos), add = TRUE)
repos[repos == "@CRAN@"] <- "https://cloud.r-project.org"
options(repos = repos)

# check for renv on CRAN matching this version
db <- as.data.frame(available.packages(), stringsAsFactors = FALSE)
if ("renv" %in% rownames(db)) {
entry <- db["renv", ]
if (identical(entry$Version, version)) {
message("* Installing renv ", version, " ... ", appendLF = FALSE)
dir.create(libpath, showWarnings = FALSE, recursive = TRUE)
utils::install.packages("renv", lib = libpath, quiet = TRUE)
message("Done!")
return(TRUE)
}
}

# try to download renv
message("* Downloading renv ", version, " ... ", appendLF = FALSE)
prefix <- "https://api.github.com"
url <- file.path(prefix, "repos/rstudio/renv/tarball", version)
destfile <- tempfile("renv-", fileext = ".tar.gz")
on.exit(unlink(destfile), add = TRUE)
utils::download.file(url, destfile = destfile, mode = "wb", quiet = TRUE)
message("Done!")

# attempt to install it into project library
message("* Installing renv ", version, " ... ", appendLF = FALSE)
dir.create(libpath, showWarnings = FALSE, recursive = TRUE)

# invoke using system2 so we can capture and report output
bin <- R.home("bin")
exe <- if (Sys.info()[["sysname"]] == "Windows") "R.exe" else "R"
r <- file.path(bin, exe)
args <- c("--vanilla", "CMD", "INSTALL", "-l", shQuote(libpath), shQuote(destfile))
output <- system2(r, args, stdout = TRUE, stderr = TRUE)
message("Done!")

# check for successful install
status <- attr(output, "status")
if (is.numeric(status) && !identical(status, 0L)) {
text <- c("Error installing renv", "=====================", output)
writeLines(text, con = stderr())
}


}

try(install_renv())

# try again to load
if (requireNamespace("renv", lib.loc = libpath, quietly = TRUE)) {
message("Successfully installed and loaded renv ", version, ".")
return(renv::load())
}

# failed to download or load renv; warn the user
msg <- c(
"Failed to find an renv installation: the project will not be loaded.",
"Use `renv::activate()` to re-initialize the project."
)

warning(paste(msg, collapse = "\n"), call. = FALSE)

})
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Scooter Rides: Weekend vs Weekday
# scooter-rides-weekend-vs-weekday.R
# Author: Owen Thompson
# Contact: [email protected]
# Date Created: 09.25.19


# 0 Set up ####
if (!require("pacman")) install.packages("pacman")
library(pacman)
p_load(tidyverse, scales, lubridate, ggthemes, ggpubr)

# Download Code for Nashville's "open-data-portal" repo from Krys Mathis
setwd("../Downloads/open-data-portal-feature-scooter-2019-09-clean-up/open-data-portal-feature-scooter-2019-09-clean-up/nashville/scooter-data/")

# 1 Data Wrangling ####

scoot <- read_csv("scooter_extract_2019-07-20_to_2019-09-09.csv",
col_types = cols(.default = col_character(),
availability_duration = col_double(),
availability_start_date = col_date(format = ""),
availability_start_time = col_time(format = ""),
gps_latitude = col_double(),
gps_longitude = col_double(),
real_time_fare = col_double(),
availability_start_date_cst = col_date(format = ""),
availability_start_time_cst = col_time(format = ""),
availability_duration_seconds = col_double(),
first_extract_date_cst = col_date(format = ""),
first_extract_time_cst = col_time(format = ""),
first_extract_date_utc = col_date(format = ""),
first_extract_time_utc = col_time(format = ""),
last_extract_date_cst = col_date(format = ""),
last_extract_time_cst = col_time(format = ""),
last_extract_date_utc = col_date(format = ""),
last_extract_time_utc = col_time(format = "")))

## Check dates of scotter rides:
##scoot %>%
## summarise(min_datetime = min(last_extract_date_cst),
## max_datetime = max(last_extract_date_cst))
## 7/20 to 9/20

explore_scoot <- scoot %>%
filter(gps_longitude < -50) %>%
arrange(sumd_id, last_extract_date_cst, last_extract_time_cst) %>%
group_by(sumd_id) %>%
mutate(id = row_number()) %>%
mutate(prev_avail_seconds = lag(availability_duration_seconds, n = 1, order_by = id)) %>%
filter(availability_duration_seconds < prev_avail_seconds) %>%
mutate(prev_lat = lag(gps_latitude, n = 1),
prev_lon = lag(gps_longitude, n = 1)) %>%
filter(!is.na(prev_lat)) %>%
ungroup() %>%
mutate(hr = hour(availability_start_time_cst)) %>%
mutate(day_type = if_else(wday(availability_start_date_cst, week_start = 1) > 5, "Weekend", "Weekday"),
dow = wday(availability_start_date_cst, label = T, abbr = F),
wk = floor_date(availability_start_date_cst, "week") %>% format("%m-%d"))

# 3 Data viz ####
explore_scoot %>%
ggplot(aes(x = hr, fill = day_type, group = last_extract_date_cst)) +
geom_histogram(bins = 24, alpha = 0.2, position = "identity") +
# stat_bin(geom = "step", bins = 23, binwidth = 1, position = "identity") +
scale_x_continuous(labels = c("5am","8am","Noon","5pm","8pm"),
breaks = c(5, 8, 12, 17, 20)) +
scale_y_continuous(labels = comma_format()) +
theme_fivethirtyeight() +
theme(strip.background = element_rect(fill = "#FFFFFF")) +
facet_wrap(.~day_type, nrow = 1) +
labs(fill = "",
title = "Scooter ride distribution\nWeekday vs. Weekend") +
scale_fill_fivethirtyeight() %>%
ggsave("scooter-rides-weekday-vs-weekend.png)