Analyzing my Uber Trips in R : Part 1

Hello Friends! Today I’ll be analyzing my Uber trips using ubeR package in R. Uber provides cool API where you can use in multiple ways. From integrating to another app to Integrating to Amazon Echo or any other smart tool. More about Uber at wikipedia

To start integration of Uber API with the R first we have to create Uber developer account. Uber API provides almost all the features which is there in app like ride estimate, book ride, trip history etc. Go to Developer and sign in with your Uber credentials. You’ll see the page like below.

1. Click on the new app to create an app.

2. Name your app and give the description.

3. Once you create the app you’ll see the developer dashboard like below. I’ve blacked out my Client ID, Client  Secret and Server Token. Never share your Client ID, Client Secret and Server Token with any one and never post anywhere. Also fill the redirected URL as shown below and that’s done. Your Uber developer account is ready to use in R.

 

Once the Uber developer app is ready, we’ll move to R and follow the below steps.

#install packages
install.packages("ubeR")
install.packages("marittr")
install.packages("dplyr")

#use libraris
library(ubeR)
library(magrittr)
library(dplyr)

Enter the Uber credentials (get from the recently created app)

#signing into your uber account

uber_oauth(UBER_CLIENTID, UBER_CLIENTSECRET)

Once you enter the details correctly the connection between R and Uber API will be completed and you are good to go. Below we are fetching the trip details from the 1st ever trip you have taken on Uber.

#fetching the trip details
trips <- data.frame()
more_trips <- TRUE
off_set <- 0
while(more_trips) {
 new_trips <- uber_history(50, 50 * off_set)
 if (is.null(new_trips)) {
 more_trips <- FALSE
 } else {
 trips <- rbind(trips, new_trips)
 off_set <- off_set + 1
 }
}

It will look like something shown below

Creating some columns for some parameters

#waiting time
trips$waiting_time<- trips$start_time-trips$request_time

#minutes and rounding off 2
trips$waiting_time<- round(trips$waiting_time/60,2)

#travel time
trips$travel_time<- trips$end_time-trips$start_time

Exporting to csv file. The file will be saved in the default directory.

#exporting to a csv file 
write.csv(trips,"Uber.csv")

 

Thanks for reading! This tutorial was about just to fetch the details in R using Uber API. I’ll post the second part of the tutorial with analysis and findings.

Reference and further reading

Comment your suggestion and queries!

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *