Advanced R Lab: Processing Citi Bike API Data, JSON Structure, and Statistical Visualization
Initial API Data Structure and Output
Understanding API Output:
The initial downloaded data from an API is essentially a long text string.
In the R environment, this output is often truncated, meaning it is shortened for display and does not show every piece of information at once.
The structure of the raw API data shows that it is a collection of various columns, though not every column provided by the API is necessarily included in the final local dataset.
JSON Conversion:
When the raw text string is converted to JSON format, the structure becomes more organized.
The conversion allows users to see specific columns identified by the
$symbol.
Utilizing the jsonlite Package and prettify Function
Examining Objects with prettify:
The
jsonlitepackage contains a custom function calledto examine API output objects.Functionality:
makes raw JSON text significantly easier to read by adding line breaks and indentation.Output Volume: Running
can generate an enormous amount of output. For instance, when knitting a lab document to HTML or PDF, it can result in a document spanning nearly 700 pages if not handled correctly.Machine Performance Warning: Older machines or those with slow processing may struggle to process the large output of the
function.Best Practice: It is advised to use
for initial analysis purposes only. After viewing the structure, the line of code should be commented out (using#) before knitting the file to prevent the computer from attempting to render hundreds of pages of text.
Data Content in the Citi Bike Dataset:
The output from
reveals individual station IDs for every entry in the Citi Bike system across New York City.Each entry contains unique identification and several datasets, including:
(Available docking slots)
Object Inspection and Descriptive Statistics
The str() Function:
The
function (Structure) is used to determine the type of object and what information is available within it.Applying
reveals that the object is a data frame containing a specific number of observations and variables.
Data Frame Composition:
Initially, the
data frame may contain a subset of variables (e.g., 6 columns) filtered from the larger original API output.
Generating Histograms:
Histograms are generated using the
function.To specify a column within a data frame, use the dollar sign notation:
(e.g.,).Live Data Variation: Because the Citi Bike API provides live data, visual outputs like histograms will vary depending on the exact time the data is pulled. For example, a histogram generated at one moment might look different from one generated five minutes later due to bike movements in the city.
Summary Statistics and Data Filtering
Counting Specific Observations:
To find how many stations meet a certain criteria, use the
function combined with a logical operator.Example: To find stations with at least one e-bike available: sum(station_status\$num_ebikes_available > 0)
Example Results from Transcript:
Instructor's result: 1,893 stations.
Sajid's result: 1,904 stations.
Another student's result: 1,890 stations.
This variation confirms the dynamic nature of the dataset during a sunny day in New York City.
Creating Subsets (New Data Frames):
To explore stations with at least one e-bike, a new data frame can be created using square bracket indexing.
Example name:
Syntax: `ebikes_df <- station_status[station_status\$num_ebikes_available > 0, ]
The resulting data frame will have fewer observations than the original
data frame because it only includes rows where the condition is true.
Feature Engineering and New Attributes
Calculating Means:
The
function can be applied to specific columns to compare data subsets.Comparing the mean of
for the filteredversus the fullhelps identify if stations with e-bikes have higher or lower dock availability on average.
Creating the station_size Column:
A new attribute called
represents the total number of slots available for bikes (occupied or empty).This is calculated by summing several existing columns:
Upon running this calculation, the environment will show the data frame variable count increasing (e.g., from 6 to 7 variables).
Calculating Occupied Docks:
A new variable,
, is created to represent the number of docks currently in use.Instructional logic for this variable:
Graphical Data Analysis: Scatter Plots
Using the plot Command:
The
function is used to produce an $x-y$ scatter plot.Mapping:
X-axis:
(Number of docks currently filled).Y-axis:
(Total number of bikes available for rent).
Visual Interpretation of the Citi Bike Scatter Plot:
Clusters: Most stations tend to cluster between $10$ to $50$ occupied docks and $5$ to $40$ available bikes.
Interpretation: This suggests many stations share a similar capacity range.
Outliers: Points located farther to the right (higher X-axis values) or near the top (higher Y-axis values) represent "larger capacity stations."
Specific Examples: Data points showing $80$ to $90$ occupied docks with nearly zero bikes available indicate stations that are effectively at capacity but currently empty of rentable bikes.
Lab Compilation and Technical Troubleshooting
Knitting the Lab:
Labs are typically knitted to HTML for submission.
Final output can be opened in a browser and then printed or "Saved as PDF."
Troubleshooting Package Errors:
Error Message: "could not find function 'fromJSON'"
Cause: This usually occurs if the
jsonlitepackage was not installed properly or the library was not loaded using.Technical fix:
Run
.Run
.Crucial Step: Comment out the
line before knitting to avoid errors during the rendering process.
Questions & Discussion
Location Narratives:
Hugo mentioned he is located in downtown New York City near Wall Street and Tribeca, noting it was a sunny day, which explains why the bike data was changing rapidly during the lab.
A student in the chat mentioned it was raining in Georgia.
Troubleshooting with Ray:
Ray encountered errors where R could not find the conversion functions.
The instructor guided Ray to check the console for installation success. It was discovered that the
jsonlitepackage had not finished installing.Ray was instructed to place the cursor at the end of the maintenance lines and use
Control + Enterto force the execution of the install and library commands.
Filtering Issues with ebikes_df:
A student encountered an error "ebikes_df not found" during step 10.
The instructor clarified that the student must successfully run the code in step 9 (the filtering step) to create the object in the R environment before it can be used for calculations in step 10.