0Pricing
R Academy · Lesson

Coordinate Reference Systems and Projections

Transform between CRS using st_crs() and st_transform() correctly.

Why CRS Matters

A Coordinate Reference System (CRS) tells R what the numbers in your geometry column mean — degrees of latitude/longitude, metres in a projected grid, or something else. Mixing data in different CRS without re-projecting causes spatial joins to fail silently and distance calculations to be wrong.

library(sf)

# Two sf objects in the same CRS: operations work
nc <- read_sf(system.file('shape/nc.shp', package = 'sf'))

# sf will error or warn when CRS do not match during operations
# This is the CRS metadata attached to the object
crs <- st_crs(nc)
cat('EPSG:', crs$epsg, '\n')
cat('Input is geographic (lon/lat)?', st_is_longlat(nc), '\n')
cat('Units:', crs$units_gdal, '\n')

st_crs: Reading CRS Info

st_crs(sf_obj) returns a crs object with EPSG code, WKT string, Proj4 string, and unit information. You can also pass an integer EPSG code directly to retrieve the CRS definition.

library(sf)

nc <- read_sf(system.file('shape/nc.shp', package = 'sf'))

# Inspect the CRS object
crs <- st_crs(nc)
cat('Class:', class(crs), '\n')
cat('EPSG:', crs$epsg, '\n')
cat('Proj4:', substr(crs$proj4string, 1, 60), '...\n')

# Lookup a CRS by EPSG code
wgs84 <- st_crs(4326)
web_mercator <- st_crs(3857)

cat('\nWGS84 name:', wgs84$Name, '\n')
cat('Web Mercator name:', web_mercator$Name, '\n')

All lessons in this course

  1. Simple Features with the sf Package
  2. Coordinate Reference Systems and Projections
  3. Spatial Joins and Operations
  4. Interactive Maps with leaflet
← Back to R Academy