10 Base R
🚩 Pre-Class Learnings
To prepare for this lesson, do the followings:
- Read Chapter 27 in D4DS
- Do checkpoint in Moodle
🔥 Data Story Critique
Go to https://www.nytimes.com/interactive/2017/08/29/opinion/climate-change-carbon-budget.html then answer the following questions:
- What is the data story?
- What is effective?
- What could be improved?
Before starting, review the ICA Instructions ⭐ for details on pair programming and activity procedures.
🧩 Learning Goals
By the end of this lesson, you should be able to:
- Identify and define the properties of common structures in R
- Subset vectors and lists with
[
by index, name, logical vector, and indirectly with objects - Subset data frames and lists with
$
and[[
- Use the
str()
function to examine the structure of an unfamiliar object and extract components from the object - Apply printing strategies to streamline the debugging and development process
Common R Object Structures
Vector: A vector is a collection of elements of the same type (e.g., numeric, integer, character, logical).
- Fun Fact: A vector can have names for each of its elements.
List: A list is a collection of elements (e.g., vectors, matrices, data frames, other lists).
- A list can have different types of elements.
- A list can have names for its elements.
Other Common R Object Structures
Array: An array is a vector with a dimension attribute.
- Like a vector, an array can only have one type of data (e.g., numeric, character).
Matrix: A matrix is an array with only 2 dimensions (rows, columns).
- Like a vector, a matrix can only have one type of data (e.g., numeric, character).
Data Frame/tibble: A data frame is a named list with elements of equal length.
- Each element is a “column” in the data frame.
- The columns can be of different types (e.g., character, numeric, logical, lists, etc.).
- Data frames are the most common way to store data in R.
- Tibbles do less and complain more than base data.frames
Base R Subsetting
The content here comes from Chapter 27 of R4DS, with some small additions.
Selecting elements with [
We can subset common R structures and maintain the class structure with [ ]
.
There are four main types of things that you can subset with, i.e., that can be the i
in x[i]
:
- A vector of positive integers. Subsetting with positive integers keeps the elements at those positions:
By repeating a position, you can actually make a longer output than input, making the term “subsetting” a bit of a misnomer.
- A vector of negative integers. Negative values drop the elements at the specified positions:
-
A logical vector. Subsetting with a logical vector only keeps values corresponding to
TRUE
. This is generally used with comparison functions and operators.
Unlike filter()
, NA
indices will be included in the output as NA
s unless you explicitly remove them (filter()
removes instances of missing values by default.
- A character vector. If you have a named vector or list, you can subset it with a character vector:
As with subsetting with positive integers, you can use a character vector to duplicate individual entries.
Be very wary of vector recycling when doing this! The number of things that you’re inserting should either be 1 or the size of the x[i]
subset.
Subsetting Matricies and Data Frames with [
All of the above subsetting options can be used for subsetting matrices and data frames (named list of elements of equal length).
You can use a comma to subset by rows and columns separately.
. . .
m[1,3] # Get 1st row and 3rd column
m[c(1,3),] # Get 1st and 3rd rows
m[,c(1,3)] # Get 1st and 3rd columns
m[c(1,3),c(1,3)] # Get 1st and 3rd rows and 1st and 3rd columns
m[-1,] # Get all rows except 1st
m[c(TRUE, FALSE, FALSE),] # Get the 1st row via a logical
# Add row and column names to the matrix
colnames(m) <- str_c("col", 1:4)
rownames(m) <- str_c("row", 1:3)
m["row1",]
Selecting a single element with $
and [[
We can use $
and [[
to extract a single column of a data frame or an element within a list. This breaks out of the original class structure.
Exercises
- Subsetting Functions For each of the tasks below, write a function that take a vector as input returns the desired output:
- The elements at even-numbered positions. (Hint: use the
seq()
function.) - Every element except the last value.
- Only even values (and no missing values).
Exploring the structure of an object with str()
The str()
function shows you the structure of an object and is useful for exploring model objects and objects created from packages that are new to you.
In the output of str()
dollar signs indicate named components of a list that can be extracted via $
or [[
.
. . .
We see that both mod
and mod_summ
are lists, so we can also interactively view these objects with View(mod)
and View(mod_summ)
in the Console.
Exercise
-
CI Function Write a function that fits a linear model on the dataset using the given outcome and predictor variables and return a data frame (
tibble
) with the coefficient estimate and CI for the predictor of interest. It should take the following inputs:
-
data
: A dataset -
yvar
: Outcome variable to be used in a linear model (a length-1 character vector) -
preds
: Predictor variables to be used in a linear model (a character vector) -
pred_of_interest
: The variable whose coefficient estimate and confidence interval are of interest (a length-1 character vector and should be one ofpreds
)
Development tip: As you develop, it will help to create objects for the arguments so that you can see what output looks like interactively:
Test your function on the mtcars
dataset.
When you’re done developing your function, remove these objects to declutter your environment by entering rm(data, yvar, preds, pred_of_interest)
in the Console.
fit_mod_and_extract <- function(___) {
# Use str_c to create a string (formula_str) that looks like "yvar ~ pred1 + pred2"
# Look at the documentation for a helpful argument
mod_formula_str <-
mod_form <- as.formula(mod_formula_str)
# Fit a linear model using the constructed formula and given data
mod <- lm(mod_form, data = data)
# Obtain 95% confidence interval
ci <- confint(mod, level = 0.95)
# Return the coefficient estimate and CI for the predictor of interest
tibble(
which_pred = pred_of_interest,
estimate = ___,
ci_lower = ___,
ci_upper = ___
)
}
Debugging Strategies
When writing functions and working with functions that you wrote, you may encounter errors that are hard to figure out.
Here are some strategies to help you debug the issues you encounter:
- Use
print()
andcat()
to print out intermediate results and messages within a function.- Examples:
print(x)
,cat("The value of x is", x, "\n")
- Examples:
- Use
browser()
to pause the function at a certain point and interactively explore the environment. Press “Next” or typen
to run the next line of code. Type the name of an object in the Console to see its value at this point in the function. You can typeQ
to quit the browser.- Example below:
fit_mod_and_extract <- function(data, yvar, preds, pred_of_interest) {
# Use str_c to create a string (formula_str) that looks like "yvar ~ pred1 + pred2"
# Look at the documentation for a helpful argument
mod_formula_str <- str_c(yvar, "~", str_c(preds, collapse = "+"))
mod_form <- as.formula(mod_formula_str)
# Add browser() to where in the function you'd like to pause and interact in the function environment using the Console
browser()
# Fit a linear model using the constructed formula and given data
mod <- lm(mod_form, data = data)
# Obtain 95% confidence interval
ci <- confint(mod, level = 0.95)
# Return the coefficient estimate and CI for the predictor of interest
tibble(
which_pred = pred_of_interest,
estimate = mod$coefficients[pred_of_interest],
ci_lower = ci[pred_of_interest, "2.5 %"],
ci_upper = ci[pred_of_interest, "97.5 %"]
)
}
fit_mod_and_extract(data = mtcars, yvar = "mpg", preds = c("hp", "wt"), pred_of_interest = "hp")
- Use
try()
to catch errors and print out a message when an error occurs.- Example below:
- Include
if else
statements within a function to ensure that you are passing the right type of input to a function. You can create you own custom error message withstop()
.- Example below:
Solutions
- Subsetting Functions
Solutions
- CI Function
Solutions
fit_mod_and_extract <- function(data, yvar, preds, pred_of_interest) {
# Use str_c to create a string (formula_str) that looks like "yvar ~ pred1 + pred2"
# Look at the documentation for a helpful argument
mod_formula_str <- str_c(yvar, "~", str_c(preds, collapse = "+"))
mod_form <- as.formula(mod_formula_str)
# Fit a linear model using the constructed formula and given data
mod <- lm(mod_form, data = data)
# Obtain 95% confidence interval
ci <- confint(mod, level = 0.95)
# Return the coefficient estimate and CI for the predictor of interest
tibble(
which_pred = pred_of_interest,
estimate = mod$coefficients[pred_of_interest],
ci_lower = ci[pred_of_interest, "2.5 %"],
ci_upper = ci[pred_of_interest, "97.5 %"]
)
}
fit_mod_and_extract(data = mtcars, yvar = "mpg", preds = c("hp", "wt"), pred_of_interest = "hp")