Analyzing MTA Bus Data

You are logged in as . Logout
Time Space Diagram using Bus Data

Load Required Libraries

library("rgl")
## Warning: package 'rgl' was built under R version 3.3.3
library("rmarkdown")
## Warning: package 'rmarkdown' was built under R version 3.3.3

Reading Bus Data

This is a Markdown document. If you are interested about how to do similar HTML pages, please visit: http://rmarkdown.rstudio.com

# Read the input into a dataframe
df <- read.csv("../Data/pings4.csv", head=TRUE, skip=1)

# Check the class of the RecordedAtTime column
class(df$RecordedAtTime)
## [1] "factor"
# convert the class of the RecordedAtTime column
df$RecordedAtTime <- as.POSIXlt(df$RecordedAtTime)

# Extract hour from the column
head(df$RecordedAtTime$hour)
## [1] 7 8 7 8 8 7

Filtering Bus Data

You can also embed plots, for example:

# Create another dataframe which will contain one bus line and one direction
# Bus line = B54
df2 <- subset(df, df$PublishedLineName == "B54")
# Direction = 0
df2 <- subset(df2, df2$DirectionRef == 0)
# Let's take a look at the data
head(df2)
##    OriginRef  Bearing Latitude Longitude    VehicleRef
## 1 MTA_308472 357.9943 40.69357  -73.9814 MTA NYCT_7268
## 2 MTA_308472 357.9943 40.69357  -73.9814 MTA NYCT_6543
## 3 MTA_308472 357.9943 40.69357  -73.9814 MTA NYCT_4447
## 4 MTA_308472 357.9943 40.69357  -73.9814 MTA NYCT_6543
## 5 MTA_308472 357.9943 40.69357  -73.9814 MTA NYCT_6543
## 6 MTA_308472 357.9943 40.69357  -73.9814 MTA NYCT_7268
##             DestinationName JourneyPatternRef      RecordedAtTime
## 1 RIDGEWOOD TERM via MYRTLE       MTA_B540062 2017-05-15 07:54:41
## 2 RIDGEWOOD TERM via MYRTLE       MTA_B540062 2017-05-15 08:00:36
## 3 RIDGEWOOD TERM via MYRTLE       MTA_B540062 2017-05-15 07:56:42
## 4 RIDGEWOOD TERM via MYRTLE       MTA_B540062 2017-05-15 08:00:36
## 5 RIDGEWOOD TERM via MYRTLE       MTA_B540062 2017-05-15 08:00:36
## 6 RIDGEWOOD TERM via MYRTLE       MTA_B540062 2017-05-15 07:54:41
##        LineRef PublishedLineName
## 1 MTA NYCT_B54               B54
## 2 MTA NYCT_B54               B54
## 3 MTA NYCT_B54               B54
## 4 MTA NYCT_B54               B54
## 5 MTA NYCT_B54               B54
## 6 MTA NYCT_B54               B54
##                       DatedVehicleJourneyRef DirectionRef
## 1 MTA NYCT_FP_B7-Weekday-SDon-046500_B54_209            0
## 2 MTA NYCT_FP_B7-Weekday-SDon-047500_B54_210            0
## 3 MTA NYCT_FP_B7-Weekday-SDon-045500_B54_206            0
## 4 MTA NYCT_FP_B7-Weekday-SDon-047500_B54_210            0
## 5 MTA NYCT_FP_B7-Weekday-SDon-047500_B54_210            0
## 6 MTA NYCT_FP_B7-Weekday-SDon-046500_B54_209            0

Creating a 3D Plot of Bus Data

# Before creating a 3D Plot, we need to order the dataframe by RecordedAtTime
df2 <- df2[with(df2, order(df2$RecordedAtTime)),]

# This function will create an interactive 3D plot
plot3d(df2$Longitude,df2$Latitude, df2$RecordedAtTime, type="l", col=as.numeric(df2$DatedVehicleJourneyRef))
rglwidget()