A map of elevators in NYC

[This article was first published on StatOfMind, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Not too long ago, I came across a random tweet pointing to a GitHub repository full of miscallaneous datasets. I was imemdiately excited by the various data science and visualization problems tha I could take on and immediately decided to get my hands dirty. One of the first datasets that I saw was a dataset called eleavtors. As stated on the github repo, it contains

A list of registered elevator devices in New York City provided by the Department of Buildings in response to a September 2015 FOIL request. The data was received on a DVD in November 2015. The spreadsheet contains information on 76,088 elevators in New York City.

As a resident of NYC, and someone who has mayny times complained abut going to friends houses with no elevators (admittedly a first-world problem…), I wanted to take a look at that data. (Full disclosure, half-way through this little project, I realized that fivethirtyeight had already beaten me to the punch and performed an analysis of its own.

A map of elevators in NYC

The main point of this post is to illustrate how simple it is to produce interactive leaflet map using the leaflet and htmlwidgets package in R. in fact, it is only a single line of code to produce the map above, the rest is just processing the data into the correct format. A word of advice when using the leaflet package at the zipcode level: the library expects 5 digits for all US-based zipcode, while many databases produce zipcodes that sometimes contain 4 digits. If that happens, it is necessary to pad “0”s to the zipcode until the string in question has a length of five. This can be achieved using a neat little trick that is shown in line 11 of the code below!


# read in prison details and zip coordinates
# load required libraries

# read in source data and process
elevators_dt <- fread('nyc_elevators.csv')
elevators_dt <- elevators_dt[elevators_dt[['STREET_NAME']] != 'DUMMY RECORD', ]

# define function to extract zip code of elevators
extract_zip <- function(x) {
    if(nchar(x) == 9) {
        return(substring(as.character(x), 1, 5))

# count frequency of elevators by zip code and coordinates
zip_cnt <- elevators_dt[, .N, by=ZIP_CODE]
zip_cnt[, 'zip' := unlist(sapply(zip_cnt$ZIP_CODE, function(x) extract_zip(x)))]
coord_pairs <- elevators_dt[, .N, by=list(LATITUDE, LONGITUDE)]

# generate interactive leaflet map of elevators in NYC
p <- leaflet(coord_pairs) %>%
     attribution='Map tiles by <a href="http://stamen.com">Stamen Design</a>,
     <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> Map data
     <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>') %>% 
     setView(-73.977896, 40.731724, zoom = 12) %>% 
     addCircles(~LONGITUDE, ~LATITUDE, weight = 3, radius=5, color="#ffa500", stroke = TRUE, fillOpacity = 0.8) %>% addLegend("bottomright", colors= "#ffa500", labels="Dunkin'", title="In Connecticut")

saveWidget(p, file='elevator_map_nyc.html')

To leave a comment for the author, please follow the link and comment on their blog: StatOfMind.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)