<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/exploring-san-francisco-bay-areas-bike-share-system/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/exploring-san-francisco-bay-areas-bike-share-system/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Congested streets and slow-crawling traffic are a fact of life in many metropolitan areas, such as New York City, Los Angeles, and Chicago. Bike sharing is an innovative solution for such problems, and it works by dispersing a large fleet of publicly-available bikes throughout crowded cities for personal transport. </p>
<p>Implemented in 2013, Ford GoBike is the first bike-sharing system introduced in the US West Coast. Its 540 stations and 7,000 bikes sprawl across five cities in San Francisco Bay Area. A docked bike can be checked out at any station and must be returned to a station when the trip is complete. As an avid GoBike user and a data fanatic, I am excited to explore, using R, the trip data that has been collected in 2018.</p>
<h2>Highlights</h2>
<ul>
<li>The number of rides per month more than doubled from January to June of 2018.</li>
<li>Monthly and yearly subscribers have completed more than five times the number of rides compared to customers who hold single-use or day-ride passes.</li>
<li>The ratio of male to female users is 3:1.</li>
<li>The median age of users is 35 with a standard deviation of 10.5.</li>
<li>More than 90% of the rides are under 30 minutes.</li>
<li>The peak hours across all rides in 2018 are 8 AM and 5 PM, which align with normal working hours.</li>
<li>The bike share system in San Francisco is popular among those who commute from outside of the city for work.</li>
</ul>
<h2>Data Acquisition</h2>
<p>First, I downloaded the <a rel="nofollow" href="https://www.fordgobike.com/system-data" target="_blank">system data</a> from the Ford GoBike website and saved them in my working directory. Currently, eight data files are available online, a collective file for 2017 and one for each month of 2018 up to August. In this analysis, I will solely focus on data that has been generated so far in 2018.</p>
<pre><code># List of data file names
file_names = list.files(pattern = &quot;.csv&quot;)
file_names</code>
<em>## [1] &quot;201801-fordgobike-tripdata.csv&quot; &quot;201802-fordgobike-tripdata.csv&quot;
## [3] &quot;201803-fordgobike-tripdata.csv&quot; &quot;201804-fordgobike-tripdata.csv&quot;
## [5] &quot;201805-fordgobike-tripdata.csv&quot; &quot;201806-fordgobike-tripdata.csv&quot;
## [7] &quot;201807-fordgobike-tripdata.csv&quot; &quot;201808-fordgobike-tripdata.csv&quot;
</em></pre>
<p>To read the files into R, I iterate through <code>file_names</code> using the <code>lapply</code> function:</p>
<pre><code># Read data (this will take a second)
df_list = lapply(file_names,
                 function(x) read.csv(x, stringsAsFactors = FALSE))

# Assign names to data frames in the list
names(df_list) = file_names
</code></pre>
<h2>Data Organization and Cleaning</h2>
<h3>Aggregate Data into a Single Data Frame</h3>
<p>Let's look at the headers of a data frame in <code>df_list</code>:</p>
<pre><code># Extract column names from the first data frame
names(df_list[[1]])</code>
<em>## [1] &quot;duration_sec&quot; &quot;start_time&quot; ## [3] &quot;end_time&quot; &quot;start_station_id&quot; ## [5] &quot;start_station_name&quot; &quot;start_station_latitude&quot; ## [7] &quot;start_station_longitude&quot; &quot;end_station_id&quot; ## [9] &quot;end_station_name&quot; &quot;end_station_latitude&quot; ## [11] &quot;end_station_longitude&quot; &quot;bike_id&quot; ## [13] &quot;user_type&quot; &quot;member_birth_year&quot; ## [15] &quot;member_gender&quot; &quot;bike_share_for_all_trip&quot;
</em></pre>
<p>Information on ride duration, starting and ending time/location, and user descriptions are provided. Notice that the data contains a column called <code>&quot;bike_share_for_all_trip&quot;</code>, which tracks members who are enrolled in the <a rel="nofollow" href="https://www.fordgobike.com/pricing/bikeshareforall" target="_blank">Bike Share for All</a> program for low-income residents.</p>
<p>We will collapse the data frames into one and examine the data structure:</p>
<pre><code># Cast certain columns as numeric to enable row binding
df_list[[6]]$start_station_id = as.numeric(df_list[[6]]$start_station_id)
df_list[[6]]$end_station_id = as.numeric(df_list[[6]]$end_station_id)
df_list[[7]]$start_station_id = as.numeric(df_list[[7]]$start_station_id)
df_list[[7]]$end_station_id = as.numeric(df_list[[7]]$end_station_id)
df_list[[8]]$start_station_id = as.numeric(df_list[[8]]$start_station_id)
df_list[[8]]$end_station_id = as.numeric(df_list[[8]]$end_station_id)

# Bind data frames by rows
library(dplyr)   # for data manipulation
df = bind_rows(df_list)

# Glimpse at df
glimpse(df)</code>
<em>## Observations: 1,210,548
## Variables: 16
## $ duration_sec </em></pre>