-
-
Save chaityacshah/f9fd866d82c7aa5121f65406e3ab6ce1 to your computer and use it in GitHub Desktop.
Code associated with blog post: http://t-redactyl.github.io/blog/2015/10/avoiding-the-crowds-in-melbourne-cbd-using-data.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
av_peds_2 <- ddply(p.subset, c("date", "collapsed_sensors_2"), summarise, | |
n_peds = sum(Hourly_Counts)) | |
# Extract weekday versus weekend | |
av_peds_2$day <- weekdays(av_peds_2$date, abbreviate = FALSE) | |
av_peds_2$weekend <- ifelse((av_peds_2$day == "Saturday" | av_peds_2$day == "Sunday"), | |
"Weekend", "Weekday") | |
av_peds_2$weekend <- as.factor(av_peds_2$weekend) | |
# Extract time of day | |
av_peds_2$time=format(as.POSIXct(av_peds_2$date, format="%Y-%m-%d %H:%M"), format="%H:%M") | |
# Collapse data to mean number of pedestrians per site, hour and weekend/weekday | |
av_peds_2 <- ddply(av_peds_2, c("collapsed_sensors_2", "time", "weekend"), summarise, | |
mean_peds = mean(n_peds), | |
n_measurements = length(n_peds), | |
sum_peds = sum(n_peds) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sum the collapsed categories | |
library(plyr) | |
av_peds <- ddply(p.subset, c("date", "collapsed_sensors"), summarise, | |
n_peds = sum(Hourly_Counts)) | |
# Extract weekday versus weekend | |
av_peds$day <- weekdays(av_peds$date, abbreviate = FALSE) | |
av_peds$weekend <- ifelse((av_peds$day == "Saturday" | av_peds$day == "Sunday"), | |
"Weekend", "Weekday") | |
av_peds$weekend <- as.factor(av_peds$weekend) | |
# Extract time of day | |
av_peds$time=format(as.POSIXct(av_peds$date, format="%Y-%m-%d %H:%M"), format="%H:%M") | |
# Collapse data to mean number of pedestrians per site, hour and weekend/weekday | |
av_peds <- ddply(av_peds, c("collapsed_sensors", "time", "weekend"), summarise, | |
mean_peds = mean(n_peds), | |
n_measurements = length(n_peds), | |
sum_peds = sum(n_peds)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
city.centre <- av_peds_2[av_peds_2$collapsed_sensors_2 %in% | |
c("Bourke Street Mall", "Chinatown-Swanston St (North)", | |
"Swanston Street South", "Melbourne Central", | |
"Flinders Street-Elizabeth/Swanston Sts", | |
"QV Market-Elizabeth St (West)"), ] | |
ggplot(data=city.centre, aes(x=time, y=mean_peds, group=collapsed_sensors_2, colour=collapsed_sensors_2)) + | |
geom_line() + | |
geom_point() + | |
facet_grid(. ~ weekend) + | |
ylab("Mean pedestrians per hour") + | |
xlab("Time of day") + | |
scale_x_discrete(breaks=c("00:00", "03:00", "06:00", "09:00", | |
"12:00", "15:00", "18:00", "21:00")) + | |
theme_bw() + | |
scale_shape_discrete(name = "City centre") + | |
scale_colour_discrete(name = "City centre") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Extract data set with just the city east sites | |
city.east <- av_peds[av_peds$collapsed_sensors %in% | |
c("Alfred Place", "Lonsdale St-Spring St (West)", | |
"Collins Place", "Flinders St-Spring Street", | |
"Bourke St-Russel St (West)", | |
"Chinatown-Lt Bourke St (South)", | |
"Lonsdale Street (South)"),] | |
library(ggplot2) | |
ggplot(data=city.east, aes(x=time, y=mean_peds, | |
group=collapsed_sensors, | |
colour=collapsed_sensors)) + | |
geom_line() + | |
geom_point() + | |
facet_grid(. ~ weekend) + | |
ylab("Mean pedestrians per hour") + | |
xlab("Time of day") + | |
scale_x_discrete(breaks=c("00:00", "03:00", "06:00", "09:00", | |
"12:00", "15:00", "18:00", "21:00")) + | |
theme_bw() + | |
scale_shape_discrete(name = "City East") + | |
scale_colour_discrete(name = "City East") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
centre.hour <- ddply(city.centre, c("time", "weekend"), summarise, | |
total_peds = sum(sum_peds), | |
total_measurements = sum(n_measurements) | |
) | |
centre.hour$av_peds <- centre.hour$total_peds / centre.hour$total_measurements | |
max.weekday <- centre.hour$time[centre.hour$av_peds == | |
max(centre.hour$av_peds[ | |
centre.hour$weekend == "Weekday"])] | |
max.weekend <- centre.hour$time[centre.hour$av_peds == | |
max(centre.hour$av_peds[ | |
centre.hour$weekend == "Weekend"])] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
east.hour <- ddply(city.east, c("time", "weekend"), summarise, | |
total_peds = sum(sum_peds), | |
total_measurements = sum(n_measurements) | |
) | |
east.hour$av_peds <- east.hour$total_peds / east.hour$total_measurements | |
max.weekday <- east.hour$time[east.hour$av_peds == | |
max(east.hour$av_peds[ | |
east.hour$weekend == "Weekday"])] | |
max.weekend <- east.hour$time[east.hour$av_peds == | |
max(east.hour$av_peds[ | |
east.hour$weekend == "Weekend"])] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
p.subset <- with(p.subset, p.subset | |
[!(date >= "2013-11-05 00:00:00" & date <= "2013-11-05 23:00:00") & | |
!(date >= "2013-12-25 00:00:00" & date <= "2013-12-26 23:00:00") & | |
!(date >= "2013-12-31 00:00:00" & date <= "2014-01-01 23:00:00") & | |
!(date >= "2014-01-27 00:00:00" & date <= "2014-01-27 23:00:00") & | |
!(date >= "2014-02-22 00:00:00" & date <= "2014-02-23 23:00:00") & | |
!(date >= "2014-03-10 00:00:00" & date <= "2014-03-10 23:00:00") & | |
!(date >= "2014-04-18 00:00:00" & date <= "2014-04-21 23:00:00") & | |
!(date >= "2014-04-25 00:00:00" & date <= "2014-04-25 23:00:00") & | |
!(date >= "2014-06-09 00:00:00" & date <= "2014-06-09 23:00:00") & | |
!(date >= "2014-09-26 00:00:00" & date <= "2014-09-26 23:00:00") & | |
!(date >= "2014-11-04 00:00:00" & date <= "2014-11-04 23:00:00") & | |
!(date >= "2014-12-25 00:00:00" & date <= "2014-12-26 23:00:00") & | |
!(date >= "2014-12-31 00:00:00" & date <= "2015-01-01 23:00:00") & | |
!(date >= "2015-01-26 00:00:00" & date <= "2015-01-26 23:00:00") & | |
!(date >= "2015-02-21 00:00:00" & date <= "2015-02-22 23:00:00") & | |
!(date >= "2015-03-09 00:00:00" & date <= "2015-03-09 23:00:00") & | |
!(date >= "2015-04-03 00:00:00" & date <= "2015-04-06 23:00:00") & | |
!(date >= "2015-04-25 00:00:00" & date <= "2015-04-25 23:00:00") & | |
!(date >= "2015-06-08 00:00:00" & date <= "2015-06-08 23:00:00") & | |
!(date >= "2015-10-02 00:00:00" & date <= "2015-10-02 23:00:00"), ]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pedestrians$date <- as.POSIXct(as.character(pedestrians$Date_Time), | |
format = "%d-%b-%Y %H:%M") | |
p.subset <- subset(pedestrians, date >= "2013-10-01 00:00:00") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pedestrians <- read.csv(url("https://data.melbourne.vic.gov.au/api/views/b2ak-trbp/rows.csv")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
p.subset <- p.subset[!p.subset$Sensor_Name %in% | |
c("Australia on Collins", "Birrarung Marr", | |
"Lygon St (East)", "Lygon Street (West)", | |
"Grattan St-Swanston St (West)", | |
"Melbourne Convention Exhibition Centre", | |
"Monash Rd-Swanston St (West)", "New Quay", | |
"Sandridge Bridge", "St Kilda-Alexandra Gardens", | |
"The Arts Centre", "Tin Alley-Swanston St (West)", | |
"Victoria Point", "Waterfront City", "Webb Bridge"), ] | |
p.subset$Sensor_Name <- as.character(p.subset$Sensor_Name) | |
p.subset$collapsed_sensors <- p.subset$Sensor_Name | |
p.subset$collapsed_sensors[ | |
p.subset$Sensor_Name == "Bourke Street Mall (North)" | | |
p.subset$Sensor_Name == "Bourke Street Mall (South)"] <- "Bourke Street Mall" | |
p.subset$collapsed_sensors[ | |
p.subset$Sensor_Name == "City Square" | | |
p.subset$Sensor_Name == "Town Hall (West)"] <- "Swanston Street South" | |
p.subset$collapsed_sensors[ | |
p.subset$Sensor_Name == "Collins Place (North)" | | |
p.subset$Sensor_Name == "Collins Place (South)"] <- "Collins Place" | |
p.subset$collapsed_sensors[ | |
p.subset$Sensor_Name == "Flinders St-Spark La" | | |
p.subset$Sensor_Name == "Flinders St-Spring St (West)"] <- "Flinders St-Spring St" | |
p.subset$collapsed_sensors[ | |
p.subset$Sensor_Name == "Flinders St-Elizabeth St (East)" | | |
p.subset$Sensor_Name == "Flinders St-Swanston St (West)" | | |
p.subset$Sensor_Name == "Flinders Street Station Underpass"] <- | |
"Flinders Street Station" | |
p.subset$collapsed_sensors[ | |
p.subset$Sensor_Name == "Southern Cross Station" | | |
p.subset$Sensor_Name == "Spencer St-Collins St (North)" | | |
p.subset$Sensor_Name == "Spencer St-Collins St (South)"] <- "Southern Cross Station" | |
p.subset$collapsed_sensors[ | |
p.subset$Sensor_Name == "Melbourne Central" | | |
p.subset$Sensor_Name == "State Library"] <- "Melbourne Central" | |
p.subset$collapsed_sensors_2 <- p.subset$collapsed_sensors | |
p.subset$collapsed_sensors_2[ | |
p.subset$Sensor_Name == "Flinders St-Elizabeth St (East)" | | |
p.subset$Sensor_Name == "Flinders St-Swanston St (West)"] <- | |
"Flinders Street-Elizabeth/Swanston Sts" | |
p.subset$collapsed_sensors_2[ | |
p.subset$Sensor_Name == "Flinders Street Station Underpass"] <- | |
"Flinders Street Station Underpass" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
head(pedestrians) | |
table(pedestrians$Sensor_Name) | |
tapply(pedestrians$Hourly_Counts, pedestrians$Sensor_Name, function(x) sum(is.na(x))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Extract data set with just the stations (except Parliament) and the surrounding sites | |
stations <- av_peds[av_peds$collapsed_sensors %in% | |
c("Flinders Street Station", "Flagstaff Station", | |
"Melbourne Central", "Southern Cross Station"), ] | |
# Graph of the average pedestrians at each station | |
library(ggplot2) | |
ggplot(data=stations, aes(x=time, y=mean_peds, | |
group=collapsed_sensors, | |
colour=collapsed_sensors)) + | |
geom_line() + | |
geom_point() + | |
facet_grid(. ~ weekend) + | |
ylab("Mean pedestrians per hour") + | |
xlab("Time of day") + | |
scale_x_discrete(breaks=c("00:00", "03:00", "06:00", "09:00", | |
"12:00", "15:00", "18:00", "21:00")) + | |
theme_bw() + | |
scale_shape_discrete(name = "CBD Train Stations") + | |
scale_colour_discrete(name = "CBD Train Stations") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
station.hour <- ddply(stations, c("time", "weekend"), summarise, | |
total_peds = sum(sum_peds), | |
total_measurements = sum(n_measurements) | |
) | |
station.hour$av_peds <- station.hour$total_peds / station.hour$total_measurements | |
max.weekday <- station.hour$time[station.hour$av_peds == | |
max(station.hour$av_peds[ | |
station.hour$weekend == "Weekday"])] | |
max.weekend <- station.hour$time[station.hour$av_peds == | |
max(station.hour$av_peds[ | |
station.hour$weekend == "Weekend"])] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment