REEU: Day 5 Notes — 2024

We are using the seminar-r kernel today

Loading the R leaflet library and sf library

library(leaflet)
library(sf)

and setting options for the display in Jupyter Lab:

options(jupyter.rich_display = T)

Here are three sample points:

testDF <- data.frame(c(40.4259, 41.8781, 39.0792), c(-86.9081, -87.6298, -84.17704))

Let’s name the columns as lat and long

names(testDF) <- c("lat", "long")

Now we can define the points to plot:

points <- st_as_sf( testDF, coords=c("long", "lat"), crs=4326)

and render the map:

addCircleMarkers(addTiles(leaflet( testDF )), radius=1)

Craigslist example

Now we can try this with Craigslist data

First we load the data.table library

library(data.table)

Now we read in some Craigslist data. This takes some time:

myDF <- fread("/anvil/projects/tdm/data/craigslist/vehicles.csv",
              stringsAsFactors = TRUE)

We can look at the head of the data:

head(myDF)

and the names of the variables in the data:

names(myDF)

Here are the Craiglist listings from Indiana:

indyDF <- subset(myDF, state=="in")

and we want to make sure that the long and lat values are not missing:

testDF <- indyDF[ (!is.na(indyDF$long)) &
                 (!is.na(indyDF$lat))]

Now we set the points to be plotted:

points <- st_as_sf( testDF, coords=c("long", "lat"), crs=4326)

and we draw the map:

addCircleMarkers(addTiles(leaflet( testDF )), radius=1)