Unit 1: R and Spatial Data Science
This unit sets up everything else in the book. It starts with what spatial data science is and why location matters. It then gets you working in R and RStudio, writing reproducible R Markdown reports, and handling R’s basic building blocks: vectors, data types, and data frames. It ends with the R tools that save you from repeating yourself: making decisions with if, repeating work with loops, and writing your own functions. By the end of the unit you can load data into R, pull out exactly the values you need, and automate the steps you would otherwise do by hand.
Chapter 1: What is Spatial Data Science?
Spatial data science uses data with locations to answer questions about the world. This chapter explains how data become information, walks through the steps of a data science project, from a question to communicating results, and shows why spatial data need special care: near things tend to be alike, patterns change with scale, and relationships change from place to place. It ends by introducing the tools used in this course: R, RStudio, the tidyverse, sf, and tmap.
Chapter 2: R, RStudio, and R Markdown
R is the language that does the computing, RStudio is the workspace where you write and run it, and R Markdown is the format that combines your writing, your code, and its results in one report. This chapter tours RStudio’s four panes, explains how knitting builds a reproducible report, and introduces variables, comments, functions, and the pipe.
Chapter 3: R Basics
R runs one line at a time, in the order you run it, so this chapter shows how to think sequentially and make sure a file will knit. It covers installing and loading packages, R’s four basic data types and how vectors hold them, good names for variables, and how to read error messages and find help.
Chapter 4: Indexing Vectors and Data Frames
Indexing selects part of a vector or a table with square brackets: by position, by name, or by a condition built from comparison and logical operators. The chapter then introduces the data frame, R’s table of variables and observations, and shows how to read a CSV file into one and select its rows and columns.
Chapter 5: Blocks, Loops, and Functions
Good programmers are productively lazy. This chapter shows how blocks group lines of code, how if, else if, and else decide whether a block runs, and how while and for loops repeat one. It compares loops with R’s vectorized functions, such as ifelse(), then takes functions apart: arguments, defaults, return values, error checks with stop(), and variable scope.
.Rmd) that mixes formatted text with
chunks of R code. Knitting it runs the code and builds a finished
report.
A sequence of values that R stores together, made with c(),
such as c(71, 75, 68).
The kind of value R is storing. The four main atomic types are logical,
integer, numeric, and character; class() tells you which
you have.
R’s table: rows are observations and columns are variables. A tibble is
the tidyverse’s version of a data frame.
Code that takes inputs (arguments), does a job, and returns an output,
such as mean(temps).
The level of detail at which you study something: addresses,
neighborhoods, counties, or states. A pattern that is clear at one scale
can vanish or reverse at another.
A family of R packages (including dplyr,
ggplot2, and readr) for reading,
cleaning, reshaping, summarizing, and plotting data, all designed to
work together.
“Simple features”: the R package for spatial vector data. An sf object
is a data frame with a geometry column holding each row’s point, line,
or polygon.
An R package for making thematic maps quickly, with sensible defaults.
To build an R Markdown file into a finished document (HTML, PDF, or
Word). Knitting reruns every code chunk from the top in a fresh R
session.
In R, a named piece of memory that holds data, such as
months <- 12. In a dataset, a measured characteristic,
such as median income; each Census variable has a code.
A note for people that R ignores. In R, a comment starts with
#.
The operator |>, which passes the result on its left as
the first argument of the function on its right. Read it as “and then.”
A bundle of code, written by other users, that adds new functions to R.
Install it once with install.packages(), then load it in
each session with library().
An operator that combines TRUE and FALSE
values: & (AND, both must be true), | (OR,
either can be true), and ! (NOT).
One thing that was measured or recorded, such as a student, a county, or
a flight. In a data frame, each row is an observation.
A plain-text table in which each line is a row and commas separate the
values. Read one into R with read.csv() or
read_csv().
Done to every element of a vector at once, without a loop. Arithmetic in
R is vectorized, so distance / air_time divides row by row.
An input to a function, written inside its parentheses.