SF OpenData - Parking meter data

Where are the Parking meters in San Fransisco?

To find data on the SF OpenData website you can click on one of the 10 topics provided or you can search. To find data about Parking meters in SF, search on Parking meters. The second result should be a link to the Parking meters dataset.

Now we are going to use the provided API to download the data into R. To get the API Endpoint, from the spreadsheet, click on Export and copy the web link.

# install.packages("RSocrata)

require("RSocrata")
## Loading required package: RSocrata
df <- read.socrata("https://data.sfgov.org/api/views/f5zc-5xs3/rows.json?accessType=DOWNLOAD")
sapply(df,class)
##     created_date     created_user       descriptio         globalid 
##      "character"      "character"      "character"      "character" 
##               id last_edited_date last_edited_user         leg_date 
##        "integer"      "character"      "character"      "character" 
##       legislatio         location         objectid             side 
##      "character"      "character"        "integer"      "character" 
##           street         the_geom       z_of_unmet 
##      "character"      "character"        "integer"
head(df)

Note the the location column has been read in as a single char value containing both the longitude and latitude. These should be separate columns and numeric. First we will transform them into numeric columns. Note that sapply is used to check the classes of the columns in the dataframe.

temp <- regmatches(df$the_geom, gregexpr("(?<=\\().*?(?=\\))", df$the_geom, perl=T))
head(temp)
## [[1]]
## [1] "-122.433183994892 37.763395937184"
## 
## [[2]]
## [1] "-122.419184862327 37.801150344628"
## 
## [[3]]
## [1] "-122.421779360358 37.802574074493"
## 
## [[4]]
## [1] "-122.436217627421 37.776156410259"
## 
## [[5]]
## [1] "-122.403474238423 37.775723147523"
## 
## [[6]]
## [1] "-122.42186669887 37.758556437318"

The above pulls out all of the non-numeric aspects of the object “the_geom”. What is left is still a single character string containing the numeric values we want. We can use the function strsplit to separate them out, then we can change them to numeric.

long <- numeric(nrow(df))
lat <- numeric(nrow(df))
for (i in 1:nrow(df)){
 long[i] <- as.numeric(strsplit(temp[[i]], " ")[[1]])[2]
 lat[i] <- as.numeric(strsplit(temp[[i]], " ")[[1]])[1]
}
df$long <- long # appends this to the existing data frame
df$lat <- lat # same as above

To simplify the dataframe we will take a subset of the columns of the original dataframe: type, latitude, and longitude. We will simplify the names of the variables/columns.

df.meters <- df[, c(12, 16, 17)] # I looked these up: side, latitude, longitude
names(df.meters) <- c("side", "latitude", "longitude")
#sapply(df.meters, class)
head(df.meters)

Now using the package ggmap we get a map and then plot the locations of the Parking meters on the map.

#install.packages("ggmap")

require(ggmap)
## Loading required package: ggmap
## Loading required package: ggplot2
m <- get_stamenmap(bbox = c(left = -122.5164, bottom = 37.7066, right = -122.3554, top = 37.8103), maptype = c("toner-lite"), zoom = 13)
## Map from URL : http://tile.stamen.com/toner-lite/13/1308/3165.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1309/3165.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1310/3165.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1311/3165.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1308/3166.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1309/3166.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1310/3166.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1311/3166.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1308/3167.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1309/3167.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1310/3167.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1311/3167.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1308/3168.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1309/3168.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1310/3168.png
## Map from URL : http://tile.stamen.com/toner-lite/13/1311/3168.png
# available colors for ggplot2  http://sape.inf.usi.ch/quick-reference/ggplot2/colour

ggmap(m, base_layer = ggplot(aes(x = longitude, y = latitude), data = df.meters))  + geom_point(color="dark blue")

ggmap(m, base_layer = ggplot(aes(x = longitude, y = latitude, colour=side), data = df.meters))  + geom_point()

Subsetting.

df.meters.side <- subset(df.meters, side=="N")
ggmap(m, base_layer = ggplot(aes(x = longitude, y = latitude), data = df.meters.side))  + geom_point(color="turquoise2")

df.meters.not.north <- subset(df.meters, side!="N")
ggmap(m, base_layer = ggplot(aes(x = longitude, y = latitude), data = df.meters.not.north))  + geom_point(color="indianred2")