REEU: Day 5 Notes — 2023

We are using the seminar-r kernel today

Loading the R leaflet library and sf library

library(leaflet)
library(sf)
R

and setting options for the display in Jupyter Lab:

options(jupyter.rich_display = T)
R

Here are three sample points:

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

Let’s name the columns as lat and long

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

Now we can define the points to plot:

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

and render the map:

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

Craigslist example

Now we can try this with Craigslist data

First we load the data.table library

library(data.table)
R

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

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

We can look at the head of the data:

head(myDF)
R

and the names of the variables in the data:

names(myDF)
R

Here are the Craiglist listings from Indiana:

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

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))]
R

Now we set the points to be plotted:

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

and we draw the map:

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