Context

This notebook documents part of a data analysis I completed in the capstone experience for the Google Data Analytics Professional Certificate program on Coursera.

You can find the Github repository for my work here.

Prepare Phase

This phase involves understanding how data is generated and collected, identifying data structure and format, and ensuring data is credible.

As part of the prepare phase, I consolidate data into a single before the process phase of the analysis.

I am using libraries from the tidyverse collection of R packages and supplementing with the aws.s3 library to download data files from the Amazon AWS, and the knitr library to format tables contained within this notebook.

packages <- c("aws.s3", "dplyr", "knitr", "stringr")

installed_packages <- packages %in% rownames(installed.packages())

if(any(installed_packages == FALSE)) {
  
  install.packages(packages[!installed_packages])
  
}

invisible(lapply(packages, library, character.only = TRUE))

Data Storage

Source data for this project is collected directly by Cyclistic and stored on the Amazon AWS here.

The following code chunk uses the aws.s3 library to create and display a data frame listing the files in the AWS bucket.

files <- as.data.frame(get_bucket("divvy-tripdata")) %>%
  rename(bucket = Bucket,
         key = Key,
         date_last_modified = LastModified,
         size_in_bytes = Size) %>%
  mutate(size_in_bytes = as.numeric(size_in_bytes),
         size_in_megabytes = round(size_in_bytes / 1e+06, 1),
         date_last_modified = as.Date(date_last_modified)) %>%
  select(bucket,
         key,
         size_in_megabytes,
         date_last_modified) %>%
  arrange(key)

kable(files,
      col.names = c("Bucket",
                    "File Name",
                    "File Size (Mb)",
                    "Date Last Modified"),
      caption = "Cyclistic Data Files",
      format.args = list(big.mark = ","),
      align = c("l", "l", "r", "r"))
Cyclistic Data Files
Bucket File Name File Size (Mb) Date Last Modified
divvy-tripdata 202004-divvy-tripdata.zip 3.3 2020-06-01
divvy-tripdata 202005-divvy-tripdata.zip 8.0 2020-06-01
divvy-tripdata 202006-divvy-tripdata.zip 14.7 2020-07-06
divvy-tripdata 202007-divvy-tripdata.zip 23.6 2020-08-12
divvy-tripdata 202008-divvy-tripdata.zip 27.9 2020-09-04
divvy-tripdata 202009-divvy-tripdata.zip 24.3 2020-10-13
divvy-tripdata 202010-divvy-tripdata.zip 17.9 2020-11-04
divvy-tripdata 202011-divvy-tripdata.zip 11.7 2020-12-04
divvy-tripdata 202012-divvy-tripdata.zip 4.8 2021-01-05
divvy-tripdata 202101-divvy-tripdata.zip 3.7 2021-02-04
divvy-tripdata 202102-divvy-tripdata.zip 1.9 2021-03-10
divvy-tripdata 202103-divvy-tripdata.zip 8.0 2021-04-08
divvy-tripdata 202104-divvy-tripdata.zip 11.8 2021-05-07
divvy-tripdata 202105-divvy-tripdata.zip 18.9 2021-06-11
divvy-tripdata 202106-divvy-tripdata.zip 26.5 2021-07-15
divvy-tripdata 202107-divvy-tripdata.zip 29.7 2021-08-14
divvy-tripdata 202108-divvy-tripdata.zip 27.9 2021-09-08
divvy-tripdata 202109-divvy-tripdata.zip 27.5 2021-10-04
divvy-tripdata 202110-divvy-tripdata.zip 23.0 2021-11-04
divvy-tripdata 202111-divvy-tripdata.zip 13.8 2021-12-08
divvy-tripdata 202112-divvy-tripdata.zip 9.3 2022-01-06
divvy-tripdata 202201-divvy-tripdata.zip 3.8 2022-02-02
divvy-tripdata 202202-divvy-tripdata.zip 4.3 2022-03-02
divvy-tripdata 202203-divvy-tripdata.zip 10.4 2022-04-06
divvy-tripdata 202204-divvy-tripdata.zip 13.4 2022-05-03
divvy-tripdata 202205-divvy-tripdata.zip 22.7 2022-06-04
divvy-tripdata 202206-divvy-tripdata.zip 26.9 2022-07-15
divvy-tripdata 202207-divvy-tripdata.zip 29.5 2022-08-05
divvy-tripdata 202208-divvy-tripdata.zip 27.1 2022-09-08
divvy-tripdata 202209-divvy-tripdata.zip 25.3 2022-10-11
divvy-tripdata 202210-divvy-tripdata.zip 20.1 2022-11-08
divvy-tripdata 202211-divvy-tripdata.zip 12.4 2022-12-05
divvy-tripdata 202212-divvy-tripdata.zip 6.7 2023-01-03
divvy-tripdata 202301-divvy-tripdata.zip 6.8 2023-02-07
divvy-tripdata 202302-divvy-tripdata.zip 7.1 2023-03-07
divvy-tripdata 202303-divvy-tripdata.zip 10.3 2023-04-06
divvy-tripdata 202304-divvy-tripdata.zip 15.4 2023-05-04
divvy-tripdata 202305-divvy-tripdata.zip 23.4 2023-06-08
divvy-tripdata 202306-divvy-tripdata.zip 25.7 2023-07-13
divvy-tripdata 202307-divvy-tripdata.zip 28.5 2023-08-14
divvy-tripdata 202308-divvy-tripdata.zip 28.6 2023-10-12
divvy-tripdata 202309-divvy-tripdata.zip 24.9 2023-10-12
divvy-tripdata 202310-divvy-tripdata.zip 19.1 2023-11-13
divvy-tripdata 202311-divvy-tripdata.zip 13.2 2023-12-05
divvy-tripdata 202312-divvy-tripdata.zip 8.4 2024-01-04
divvy-tripdata Divvy_Stations_Trips_2013.zip 14.6 2020-01-24
divvy-tripdata Divvy_Stations_Trips_2014_Q1Q2.zip 18.0 2020-01-24
divvy-tripdata Divvy_Stations_Trips_2014_Q3Q4.zip 30.8 2020-01-24
divvy-tripdata Divvy_Trips_2015-Q1Q2.zip 22.4 2020-01-24
divvy-tripdata Divvy_Trips_2015_Q3Q4.zip 43.6 2020-01-24
divvy-tripdata Divvy_Trips_2016_Q1Q2.zip 31.0 2020-01-24
divvy-tripdata Divvy_Trips_2016_Q3Q4.zip 57.2 2020-01-24
divvy-tripdata Divvy_Trips_2017_Q1Q2.zip 42.0 2020-01-24
divvy-tripdata Divvy_Trips_2017_Q3Q4.zip 57.5 2020-01-24
divvy-tripdata Divvy_Trips_2018_Q1.zip 10.0 2020-01-24
divvy-tripdata Divvy_Trips_2018_Q2.zip 27.2 2020-01-24
divvy-tripdata Divvy_Trips_2018_Q3.zip 38.8 2020-01-24
divvy-tripdata Divvy_Trips_2018_Q4.zip 16.7 2020-01-24
divvy-tripdata Divvy_Trips_2019_Q1.zip 9.6 2021-11-08
divvy-tripdata Divvy_Trips_2019_Q2.zip 28.7 2021-11-08
divvy-tripdata Divvy_Trips_2019_Q3.zip 42.4 2020-01-24
divvy-tripdata Divvy_Trips_2019_Q4.zip 18.4 2020-01-24
divvy-tripdata Divvy_Trips_2020_Q1.zip 15.9 2020-05-27
divvy-tripdata index.html 0.0 2020-01-24

The bucket contains 64 zip files with last modified dates ranging from 2020-01-24 to 2024-01-04.

There are some inconsistencies in files names and time periods covered, e.g. some monthly and others quarterly. Data is available for January 2013 through November 2023.

Data Collection

I will use the most recent full year of available data for this analysis, i.e. the 2022 calendar year, to ensure I capture trends and/or seasonality. When data becomes available for December 2023, I can refresh and compare 2022 and 2023 analyses.

The code chunk below saves the zip and source files in project subfolders.

I am using a sub-string to filter out all files except those with 2022 in the file name.

target_files <- files %>%
  filter(str_sub(key, 1, 4) == "2022")

for(i in 1:nrow(target_files)) {

  save_object(object = target_files[i,]$key,
              bucket = target_files[i,]$bucket,
              file = paste("../inst/extdata/zip/", target_files[i,]$key, sep = ""),
              overwrite = TRUE)
  
}

The following code chunk extracts the source files from the zip files and saves them.

source_files <- list.files("../inst/extdata/zip", full.names = TRUE)

for(i in 1:length(source_files)) {

  unzip(zipfile = source_files[i],
        exdir = "../inst/extdata/source",
        overwrite = TRUE)
  
}

To cleanup the source directory, I remove extracted MacOS versions of the source files, which I don’t need, and rename the September 2022 file to be consistent with the others.

The source .csv files follow the naming convention: YYYYMM-divvy-tripdata.csv.

unlink("../inst/extdata/source/__MACOSX", recursive = TRUE)

file.rename("../inst/extdata/source/202209-divvy-publictripdata.csv",
            "../inst/extdata/source/202209-divvy-tripdata.csv")
## [1] TRUE

There are 12 files, one for each month in 2022.

On inspection, source files appear to be consistent in structure and can be combined without error.

The following code reads and combines the source files into a single data frame for purposes of review, cleaning and transformation. The data frame is saved in the project as an .rda file. The code also row counts of input and output files for comparison.

source_files <- list.files("../inst/extdata/source", full.names = TRUE, pattern = "\\.csv$")

source <- data.frame()

recon <- data.frame(input_file = character(0),
                    count = integer(0),
                    cumulative_count = integer(0),
                    source_count = integer(0),
                    file_size = numeric(0))

cumulative_count <- 0

for(i in 1:length(source_files)) {

  data <- read.csv(source_files[i],
                   header = TRUE,
                   sep = ",",
                   na.strings = c(""),
                   stringsAsFactors = FALSE)

  source <- rbind(source, data)

  count <- nrow(data)

  cumulative_count <- cumulative_count + count

  source_count <- nrow(source)
  
  file_size <- file.info(source_files[i])$size / 1e+06
  
  recon <- recon %>%
    add_row(input_file = source_files[i],
            count = count,
            cumulative_count = cumulative_count,
            source_count = source_count,
            file_size = file_size)

}

kable(recon,
      col.names = c("Input File",
                    "Input File Count",
                    "Cumulative Input File Count",
                    "Output File Count",
                    "Input File Size (Mb)"),
      caption = "File Reconciliation",
      format.args = list(big.mark = ","),
      align = c("l", "r", "r", "r", "r"))
File Reconciliation
Input File Input File Count Cumulative Input File Count Output File Count Input File Size (Mb)
../inst/extdata/source/202201-divvy-tripdata.csv 103,770 103,770 103,770 19.01196
../inst/extdata/source/202202-divvy-tripdata.csv 115,609 219,379 219,379 21.13252
../inst/extdata/source/202203-divvy-tripdata.csv 284,042 503,421 503,421 51.74547
../inst/extdata/source/202204-divvy-tripdata.csv 371,249 874,670 874,670 66.90909
../inst/extdata/source/202205-divvy-tripdata.csv 634,858 1,509,528 1,509,528 117.53443
../inst/extdata/source/202206-divvy-tripdata.csv 769,204 2,278,732 2,278,732 143.59264
../inst/extdata/source/202207-divvy-tripdata.csv 823,488 3,102,220 3,102,220 152.88875
../inst/extdata/source/202208-divvy-tripdata.csv 785,932 3,888,152 3,888,152 145.55944
../inst/extdata/source/202209-divvy-tripdata.csv 701,339 4,589,491 4,589,491 141.44953
../inst/extdata/source/202210-divvy-tripdata.csv 558,685 5,148,176 5,148,176 111.91529
../inst/extdata/source/202211-divvy-tripdata.csv 337,735 5,485,911 5,485,911 67.93963
../inst/extdata/source/202212-divvy-tripdata.csv 181,806 5,667,717 5,667,717 36.46659
usethis::use_data(source, overwrite = TRUE)
## ✔ Setting active project to 'C:/R/Packages/cyclistic'
## ✔ Saving 'source' to 'data/source.rda'
## • Document your data (see 'https://r-pkgs.org/data.html')

The file reconciliation table compares and confirms row counts between the input and output files.

I will explore whether I can model data to reduce overall size as part of next phase of the analysis.

Data Structure

The combined data frame includes 13 columns and 5,667,717 rows of data. The table below shows the structure of the data frame.

structure <- data.frame(variable = names(source),
                        class = sapply(source, typeof),
                        first_values = sapply(source, function(x) paste0(head(x),  collapse = ", ")),
                        row.names = NULL)

kable(structure,
      col.names = c("Variable",
                    "Type",
                    "First Values"),
      caption = "Source Data Structure",
      format.args = list(big.mark = ","))
Source Data Structure
Variable Type First Values
ride_id character C2F7DD78E82EC875, A6CF8980A652D272, BD0F91DFF741C66D, CBB80ED419105406, DDC963BFDDA51EEA, A39C6F6CC0586C0B
rideable_type character electric_bike, electric_bike, classic_bike, classic_bike, classic_bike, classic_bike
started_at character 2022-01-13 11:59:47, 2022-01-10 08:41:56, 2022-01-25 04:53:40, 2022-01-04 00:18:04, 2022-01-20 01:31:10, 2022-01-11 18:48:09
ended_at character 2022-01-13 12:02:44, 2022-01-10 08:46:17, 2022-01-25 04:58:01, 2022-01-04 00:33:00, 2022-01-20 01:37:12, 2022-01-11 18:51:31
start_station_name character Glenwood Ave & Touhy Ave, Glenwood Ave & Touhy Ave, Sheffield Ave & Fullerton Ave, Clark St & Bryn Mawr Ave, Michigan Ave & Jackson Blvd, Wood St & Chicago Ave
start_station_id character 525, 525, TA1306000016, KA1504000151, TA1309000002, 637
end_station_name character Clark St & Touhy Ave, Clark St & Touhy Ave, Greenview Ave & Fullerton Ave, Paulina St & Montrose Ave, State St & Randolph St, Honore St & Division St
end_station_id character RP-007, RP-007, TA1307000001, TA1309000021, TA1305000029, TA1305000034
start_lat double 42.0128005, 42.012763, 41.9256018819, 41.983593, 41.87785, 41.895634
start_lng double -87.665906, -87.6659675, -87.6537080423, -87.669154, -87.62408, -87.672069
end_lat double 42.01256011541, 42.01256011541, 41.92533, 41.961507, 41.8846210725794, 41.903119
end_lng double -87.6743671152, -87.6743671152, -87.6658, -87.671387, -87.6278342306614, -87.673935
member_casual character casual, casual, member, casual, member, member

Some initial observations about the data frame:

  • ride_id appears to be a unique identifier,
  • started_at and ended_at are structured as date/time but read as character fields,
  • start_station_id and end_station_id read as character fields and appear to be a mix of numeric and non-numeric characters,
  • station-related fields may be redundant and I might be able to reduce the overall size of data by organizing the data into three related tables: trip, trip_station and station.
  • a trip_station table would keep track of which station was used for start and end of each unique trip,
  • I need to check that each station has a unique id and a single value for latitude and longitude, and
  • I also need to check that start and end date/time are logical, i.e. no start dates occurring after end dates, etc.

Next Steps

The next step in this project is to process data in preparation for analysis.