Foundations of
Data Science

Spring 2026 | Data 2 (399)
Jeffrey M. Girard | Lecture 03c

Roadmap

  1. Store text/character data in strings and character vectors

  2. Install, load, and use data and functions from R packages

Strings

Strings

  • When talking to R, we need a way to distinguish
    • Object/function names (e.g., the mean function)
    • Text/character data (e.g., the letters mean)
  • Strings are R’s way of storing text data
    • Strings can store any characters (no rules!)
    • Strings are created and displayed with quotes
  • R has great tools for working with strings
    • Strings can be collected into vectors
    • Special functions can transform strings

name <- "John Doe"

Strings and Character Vectors

# Don't try to store text data without strings
my_name <- Jeff Girard
Error in parse(text = input): <text>:2:17: unexpected symbol
1: # Don't try to store text data without strings
2: my_name <- Jeff Girard
                   ^
# Instead, put it in quotation marks
my_name <- "Jeff Girard"
my_name
[1] "Jeff Girard"
# Or collect multiple strings into a vector
dyes <- c("red#40", "blue#01", "green#03")
dyes
[1] "red#40"   "blue#01"  "green#03"

Quotation Marks and Escaping

# Don't try to include quotation marks as normal
wilde <- ""Be yourself; everyone else is already taken." -- Oscar Wilde"
Error in parse(text = input): <text>:2:12: unexpected symbol
1: # Don't try to include quotation marks as normal
2: wilde <- ""Be
              ^
# Instead, escape the inner quotation marks using \"
wilde <- "\"Be yourself; everyone else is already taken.\" -- Oscar Wilde"
wilde
[1] "\"Be yourself; everyone else is already taken.\" -- Oscar Wilde"
# If you render the string, the escape-slashes will be hidden
writeLines(wilde)
"Be yourself; everyone else is already taken." -- Oscar Wilde

String-specific Functions

dyes <- c("red#40", "blue#01", "green#03")
# Some functions still work like length()
length(dyes)
[1] 3
# Count the number of characters in each string
nchar(dyes)
[1] 6 7 8
# Transform all letters to uppercase versions
toupper(dyes)
[1] "RED#40"   "BLUE#01"  "GREEN#03"

We will learn more string functions in Unit B

Packages

Packages

  • Cookbooks are a great way to learn to cook
    • They contain lots of recipes and instructions
    • Browse an online bookstore for a cookbook
    • Order it to add it to your personal bookshelf
    • To use, pull the cookbook off the shelf
  • Packages are like cookbooks for R
    • They contain helpful functions and datasets
    • Browse an online repository for a package
    • Install it to add it to your personal library
    • To use, load the package from the library

library("pkg_name")

Installing a Package

  1. Option 1 (button)
    • RStudio > Packages tab > Install button
  2. Option 2 (command)
    • install.packages("pkg_name") in R Console
    • Do not use this command in a Quarto document

Using a Package

  1. Option 1 (linking)
    • Refer to the package explicitly: pkg_name::fn_name()
    • No loading needed, but must include this every time
  2. Option 2 (loading)
    • In Quarto (once at top), type library("pkg_name")
    • Refer to the package implicitly: fn_name()

Examples

# Install using command (don't put this in Quarto)
install.packages("stringr")
# Don't try to access it without loading or linking
str_to_title("The lord of the rings")
Error in `str_to_title()`:
! could not find function "str_to_title"
# Access using Option 1 (linking)
stringr::str_to_title("The lord of the rings")
[1] "The Lord Of The Rings"
# Access using Option 2 (loading)
library("stringr")
str_to_title("The lord of the rings")
[1] "The Lord Of The Rings"

Updating Packages

  • Packages update just like R and RStudio
  • Updates add bug fixes and new features
  • RStudio makes it easy to install updates
  • Do this every few weeks or months
  1. RStudio > Session menu > Restart R option
  2. RStudio > Packages tab > Update button
  3. Select All button > Install Updates button

Learning More

  • Many packages have “vignettes” (i.e., tutorial articles)
  • Find them using browseVignettes("pkg_name")
    • Put this command in the Console, not in Quarto
    • e.g., try browseVignettes("stringr")
  • Many packages also have useful reference websites