Spatial Analysis Exercises in R: 15 Practice Problems

Fifteen practice problems on spatial analysis in R with sf and raster: read, project, geometry operations, joins, choropleth maps.

library(sf)
library(dplyr)
library(ggplot2)

Exercise 1: Read shapefile

Difficulty: Intermediate.

Show solution
nc <- st_read(system.file("shape/nc.shp", package="sf"))
head(nc)

Exercise 2: Check CRS

Difficulty: Intermediate.

Show solution
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
st_crs(nc)

Exercise 3: Transform CRS

Difficulty: Advanced.

Show solution
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
st_transform(nc, 3857) |> head(1)

Exercise 4: Plot

Difficulty: Beginner.

Show solution
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
plot(st_geometry(nc))

Exercise 5: Choropleth with ggplot

Difficulty: Intermediate.

Show solution
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
ggplot2::ggplot(nc) + ggplot2::geom_sf(ggplot2::aes(fill = AREA))

Exercise 6: Centroid

Difficulty: Intermediate.

Show solution
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
st_centroid(nc) |> head(1)

Exercise 7: Compute areas

Difficulty: Intermediate.

Show solution
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
st_area(nc) |> head()

Exercise 8: Buffer

Difficulty: Advanced.

Show solution
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
st_buffer(nc, dist = 0.05) |> head(1)

Exercise 9: Spatial join (points in polygons)

Difficulty: Advanced.

Show solution
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
pts <- st_sample(nc, 10)
st_intersects(pts, nc)

Exercise 10: Union

Difficulty: Advanced.

Show solution
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
st_union(nc) |> plot()

Exercise 11: Distance between two points

Difficulty: Intermediate.

Show solution
p1 <- st_point(c(-74, 40.7))
p2 <- st_point(c(-118, 34.0))
st_sfc(p1, p2, crs = 4326) |> st_distance()

Exercise 12: Filter by attribute

Difficulty: Beginner.

Show solution
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
nc |> filter(AREA > 0.1)

Exercise 13: Write shapefile

Difficulty: Intermediate.

Show solution
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
st_write(nc, "out.gpkg", append = FALSE)

Exercise 14: Bounding box

Difficulty: Beginner.

Show solution
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
st_bbox(nc)

Exercise 15: Raster basics

Difficulty: Advanced.

Show solution
# library(terra)
# r <- rast(system.file("ex/elev.tif", package = "terra"))
# plot(r)

What to do next

  • leaflet-Exercises (shipped), interactive maps.
  • Data-Visualization-Exercises (shipped), viz fundamentals.

Ready to earn the Spatial Analysis Certificate?

The quiz is concept-based and respects your time: pass it once and your verifiable certificate is yours to share on LinkedIn, your resume, or your portfolio. Take it when you feel comfortable with the material.

Attempt the quiz