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:
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 <-10my_varΔ±able
Tweak each of the following R commands so that they run correctly:
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:
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.