Exercise 01

Coding basics and exploring sf objects

Modified

September 5, 2024

Practice these skills
  • Confirm that you understand some coding basics
  • Use the ? operator to access function and package documentation
  • Use class(), names(), attributes(), and summary() functions to explore sf and sfc objects
  • Use the base plot() function to visualize a sf object

1 Getting started

1.1 Packages

You need the {sf}, {tidyverse}, and {spData} packages installed to complete this exercise. If they aren’t installed, install them now by copying the following lines into the console to install all three packages (don’t forget to restart your session afterwards):

install.packages(c("tidyverse", "sf", "spData"))

After restarting make sure to load the sf and tidyverse library:

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
βœ” dplyr     1.1.4     βœ” readr     2.1.5
βœ” forcats   1.0.0     βœ” stringr   1.5.1
βœ” ggplot2   3.5.1     βœ” tibble    3.2.1
βœ” lubridate 1.9.3     βœ” tidyr     1.3.1
βœ” purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
βœ– dplyr::filter() masks stats::filter()
βœ– dplyr::lag()    masks stats::lag()
β„Ή Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE

1.2 Datas

This exercise uses the nc North Carolina SIDS dataset that is included with the sf package. We can use the sf::st_read() function to load it from a shapefile:

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), as_tibble = TRUE)
Reading layer `nc' from data source 
  `/Users/elipousson/Library/R/arm64/4.4/library/sf/shape/nc.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 100 features and 14 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
Geodetic CRS:  NAD27

2 Exercises

This week’s exercise is mostly β€œfill in the blank” questions and coding exercises. You can expect fewer hints and more freedom to experiment in future exercises!

There are periodic reminders in this assignment to remind you to render, commit, and push your changes to GitHub as you complete the exercise. You should have at least three commits with meaningful commit messages by the end of the assignment. Take a look at the RStudio documentation on version control for more details on how to commit and push changes.

2.1 Coding basics

The exercises in this section are from Ch. 3 Workflow: basics in R for Data Science (2e). If you have any trouble with the next couple exercises, please review the chapter before continuing to the next section.

Why does this code not work? ____

my_variable <- 10
my_varΔ±able

Tweak each of the following R commands so that they run correctly:

libary(todyverse)

ggplot(dTA = mpg) + 
  geom_point(maping = aes(x = displ y = hwy)) +
  geom_smooth(method = "lm)

Press βŒ₯ + ⇧ + K or βŽ‡ + ⇧ + K.

What happens? ____

How can you get to the same place using the menus? ____


Render, commit, and push your changes to GitHub with the commit message β€œAdded answers for coding basics questions”.

Make sure to commit and push all changed files so that your Git pane is empty afterwards.

2.2 Look up documentation

? is an operator that you can use to pull up documentation on a function, dataset, or other topic in the Help tab pane of the RStudio IDE. For example, running the code: ?sf::st_read will pull up the documentation on a set of functions for read simple features or layers from a file or database.

Use ? to access the documentation on sf::st_geometry() then look for the β€œValue” heading that provides information about the value returned by the function.

What type of object does sf::st_geometry() return? ____

What is one other function documented on the same page? ____

Tip

Review Ch. 9 Workflow: getting help in R for Data Science (2e) for more information about getting help when you struggle with a package, function, or project.

2.3 Explore sf and sfc objects

Every object in R has at least one class:

class(1)
[1] "numeric"
class("A")
[1] "character"
class(TRUE)
[1] "logical"

Vectors, lists, data frames, and other objects can also have names and attributes. This is a named character vector:

names(c("A" = "apple"))
[1] "A"

A sf object is a data.frame but it also has a special sf class that is designed to implement the formal simple features standard in R:

class(nc)
[1] "sf"         "tbl_df"     "tbl"        "data.frame"

As a data frame, a sf object has column names (which you can access with names() or colnames()):

colnames(nc)
 [1] "AREA"      "PERIMETER" "CNTY_"     "CNTY_ID"   "NAME"      "FIPS"     
 [7] "FIPSNO"    "CRESS_ID"  "BIR74"     "SID74"     "NWBIR74"   "BIR79"    
[13] "SID79"     "NWBIR79"   "geometry" 

Next, we should run st_geometry() on nc:

nc_geometry <- st_geometry(nc)

Now, use class() to find out what type of object st_geometry() returned:

____

This object is a special type of list. You can subset one or more items from a list using a single or double set of square brackets like this:

nc_geometry[1]
Geometry set for 1 feature 
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -81.74107 ymin: 36.23436 xmax: -81.23989 ymax: 36.58965
Geodetic CRS:  NAD27
MULTIPOLYGON (((-81.47276 36.23436, -81.54084 3...
nc_geometry[[1]]
MULTIPOLYGON (((-81.47276 36.23436, -81.54084 36.27251, -81.56198 36.27359, -81.63306 36.34069, -81.74107 36.39178, -81.69828 36.47178, -81.7028 36.51934, -81.67 36.58965, -81.3453 36.57286, -81.34754 36.53791, -81.32478 36.51368, -81.31332 36.4807, -81.26624 36.43721, -81.26284 36.40504, -81.24069 36.37942, -81.23989 36.36536, -81.26424 36.35241, -81.32899 36.3635, -81.36137 36.35316, -81.36569 36.33905, -81.35413 36.29972, -81.36745 36.2787, -81.40639 36.28505, -81.41233 36.26729, -81.43104 36.26072, -81.45289 36.23959, -81.47276 36.23436)))

Take a look at the attributes for this list using attributes():

____(nc_geometry)

Now, use class() one more time to find out what type of objects make up nc_geometry:

____(nc_geometry[[1]])

summary() is another way to get information about names and attributes all at once. Try using summary() on nc_geometry:

summary(____)

Review the results and try to answer the following:

  • What is the geometry type? ____
  • What is the coordinate reference system? ____
  • How many features? ____

Now is another good time to render, commit, and push your changes to GitHub with a meaningful commit message.

Once again, make sure to commit and push all changed files so that your Git pane is empty afterwards.

2.4 Visualize sf objects

Some packages come with data we can use as soon as the package is loaded.

Load the {spData} packages using library() then make a plot of the us_states data:

library(____)

plot(____)

Can you use plot() to make a map of states color-coded by region?

plot(____)

Optional: can you use ggplot() and geom_sf() to create a plot of us_states color-coded by total population in 2015? Here is part of the code to start with:

ggplot() +
  geom_sf()

Render, commit, and push your final changes to GitHub with a meaningful commit message.

Make sure to commit and push all changed files so that your Git pane is empty afterwards.