# Taylor Swift's album "1989" sold approximately 10 million copies in its first year
albums_sold <- 10000000
# Print the value
albums_sold[1] 1e+07
BI 255 · Bethune-Cookman University · Fall 2026
Dr. Rosie Stanbrook-Buyer
August 19, 2026
Today we’re going to learn the fundamental building blocks of R — variables, data types, and basic functions — using data from one of the best-selling music artists of all time: Taylor Swift.
Her albums have sold hundreds of millions of copies, broken streaming records, and generated billions of dollars. That data is perfect for learning R.
By the end of this lab you will be able to:
<-print(), class(), nchar(), and paste()In R, a variable is a named container that holds a value. You create one using the assignment operator <-.
Think of it like giving a nickname to a piece of information so you can use it again later.
[1] 1e+07
[1] 2077
. and _MyVar and myvar are different thingsalbum_sales is better than xGood: album_sales, tour_revenue_2024, track.count Bad: 1album, my variable, x
# Fill in the blanks — Taylor Swift has released these studio albums:
# Fearless (2008), Speak Now (2010), Red (2012), 1989 (2014),
# Reputation (2017), Lover (2019), Folklore (2020), Evermore (2020),
# Midnights (2022), The Tortured Poets Department (2024)
number_of_studio_albums <- 10
# How many albums did she release between 2020 and 2024?
recent_albums <- 3 # Folklore, Evermore, Midnights (TTPD releases 2024)
recent_albums[1] 3
Not all information is the same kind. R recognizes different data types. The three most important for you right now are:
| Type | What it stores | Example |
|---|---|---|
| numeric | Numbers (with or without decimals) | 3.14, 42, 1000000 |
| character | Text (always in quotes) | "Taylor Swift", "Folklore" |
| logical | TRUE or FALSE only | TRUE, FALSE |
Use class() to ask R what type something is:
If you type Taylor Swift without quotes, R thinks you’re referring to two variables named Taylor and Swift — which don’t exist! You’ll get an error.
Always wrap text in double quotes " " or single quotes ' '.
[1] "character"
[1] FALSE
[1] TRUE
[1] 10944000
[1] "10,944,000"
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 10 - 4 |
6 |
* |
Multiplication | 6 * 7 |
42 |
/ |
Division | 20 / 4 |
5 |
^ |
Power | 2^10 |
1024 |
%% |
Remainder | 17 %% 5 |
2 |
[1] 2779776000
[1] 2.779776
[1] 29
[1] "Taylor Swift released The Tortured Poets Department in 2024"
[1] "Track_1" "Track_2" "Track_3" "Track_4" "Track_5"
paste() vs paste0()
paste() puts a space between items by defaultpaste0() puts nothing between itemsYou can also change the separator in paste(): paste("a", "b", "c", sep = "-") gives "a-b-c"
Logical values (TRUE / FALSE) are the result of asking R a yes/no question. They are the foundation of filtering data — which you’ll use constantly.
[1] TRUE
= vs ==
This is one of the most common beginner mistakes in R!
<- or = — assigns a value to a variable== — tests whether two things are equal (asks a question)x <- 5 → puts 5 into x x == 5 → asks “is x equal to 5?” and returns TRUE or FALSE
[1] TRUE
R comes with hundreds of built-in functions. Here are a few you’ll use all the time:
[1] 603.3
[1] 100.55
[1] 89.4
[1] 110.3
[1] 89.4 110.3
[1] 6
[1] 100.6
Let’s write a small summary of Taylor Swift’s career using everything we’ve learned:
Artist: Taylor Swift
Career length: 20 years
Studio albums: 10
Grammy Awards: 14
Eras Tour revenue: $ 2,077,000,000
Is she a Grammy winner? TRUE
cat()?
cat() (“concatenate and print”) is used to print formatted output. \n means “new line.” It’s great for making readable summaries.
Close your notes. Answer these on your own — you have 3 minutes. We’ll go through the answers together.
1. What does class("Fearless") return?
a) `"numeric"` b) `"logical"` c) `"character"` d) `"text"`
2. What is the key difference between = and == in R?
3. Taylor Swift has 14 Grammy Awards. Which line of code correctly stores this?
a) `grammys == 14` b) `14 <- grammys` c) `grammys <- 14` d) `grammys = "14"`
4. What does nchar("Midnights") return?
a) `1` b) `9` c) `"Midnights"` d) `"character"`
5. True or False: is.numeric("42") returns TRUE.
1. c) "character" — Any value in quotes is a character (text) type, regardless of what it says.
2. = (or <-) assigns a value to a variable. == tests whether two values are equal and returns TRUE or FALSE. This is one of the most common beginner mistakes.
3. c) grammys <- 14 — Assignment goes left: the name on the left, the value on the right. Option d) is wrong because "14" is stored as text, not a number.
4. b) 9 — “Midnights” has 9 characters (M-i-d-n-i-g-h-t-s).
5. False — "42" is in quotes, so it is a character, not a numeric. is.numeric("42") returns FALSE. You would need as.numeric("42") to convert it first.
Before you leave, make sure you can:
Choose a different music artist. Create variables for their name, debut year, number of albums, and approximate total streams. Use cat() to print a formatted career summary. Add it to your Quarto document and render it!
= and ==Quiz 1 is in class this Friday (Aug 21). It will cover: - Basic R syntax and operators - Variable assignment - Data types (numeric, character, logical) - Simple functions (class(), mean(), paste(), etc.)
Review your notes and practice in the console!
---
title: "Lab 2: Basic Building Blocks in R"
subtitle: "BI 255 · Bethune-Cookman University · Fall 2026"
author: "Dr. Rosie Stanbrook-Buyer"
date: "August 19, 2026"
format:
html:
theme: cosmo
toc: true
toc-location: left
toc-title: "In This Lab"
toc-depth: 3
code-fold: false
code-tools: true
highlight-style: github
smooth-scroll: true
embed-resources: true
callout-appearance: default
execute:
warning: false
message: false
echo: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
------------------------------------------------------------------------
## Learning R with Taylor Swift
Today we're going to learn the fundamental building blocks of R — variables, data types, and basic functions — using data from one of the best-selling music artists of all time: **Taylor Swift**.
Her albums have sold hundreds of millions of copies, broken streaming records, and generated billions of dollars. That data is perfect for learning R.
::: callout-note
## Learning Objectives
By the end of this lab you will be able to:
1. Assign values to variables using `<-`
2. Identify the three main data types: numeric, character, and logical
3. Use built-in R functions like `print()`, `class()`, `nchar()`, and `paste()`
4. Perform arithmetic and comparisons on variables
:::
------------------------------------------------------------------------
## Part 1: Variables — Storing Information
In R, a **variable** is a named container that holds a value. You create one using the **assignment operator** `<-`.
Think of it like giving a nickname to a piece of information so you can use it again later.
```{r}
# Taylor Swift's album "1989" sold approximately 10 million copies in its first year
albums_sold <- 10000000
# Print the value
albums_sold
```
```{r}
# Her Eras Tour gross revenue (in USD) as of 2024
eras_tour_revenue <- 2077000000
# You can do math with variables
eras_tour_revenue / 1000000 # Convert to millions
```
::: callout-tip
## ️ Naming Rules for Variables
- Names can contain letters, numbers, `.` and `_`
- Names **cannot** start with a number
- R is **case-sensitive**: `MyVar` and `myvar` are different things
- Use descriptive names: `album_sales` is better than `x`
Good: `album_sales`, `tour_revenue_2024`, `track.count`
Bad: `1album`, `my variable`, `x`
:::
### Your Turn
```{r}
# Fill in the blanks — Taylor Swift has released these studio albums:
# Fearless (2008), Speak Now (2010), Red (2012), 1989 (2014),
# Reputation (2017), Lover (2019), Folklore (2020), Evermore (2020),
# Midnights (2022), The Tortured Poets Department (2024)
number_of_studio_albums <- 10
# How many albums did she release between 2020 and 2024?
recent_albums <- 3 # Folklore, Evermore, Midnights (TTPD releases 2024)
recent_albums
```
------------------------------------------------------------------------
## Part 2: Data Types — What Kind of Information?
Not all information is the same kind. R recognizes different **data types**. The three most important for you right now are:
| Type | What it stores | Example |
|------|---------------|---------|
| **numeric** | Numbers (with or without decimals) | `3.14`, `42`, `1000000` |
| **character** | Text (always in quotes) | `"Taylor Swift"`, `"Folklore"` |
| **logical** | TRUE or FALSE only | `TRUE`, `FALSE` |
Use `class()` to ask R what type something is:
```{r}
# Numeric
streams <- 1500000000
class(streams)
```
```{r}
# Character
artist_name <- "Taylor Swift"
class(artist_name)
```
```{r}
# Logical
is_grammy_winner <- TRUE
class(is_grammy_winner)
```
::: callout-warning
## ️ Common Mistake: Forgetting Quotes
If you type `Taylor Swift` without quotes, R thinks you're referring to two variables named `Taylor` and `Swift` — which don't exist! You'll get an error.
Always wrap text in **double quotes** `" "` or **single quotes** `' '`.
:::
### Checking and Converting Types
```{r}
# is.numeric() asks "is this a number?" and returns TRUE or FALSE
is.numeric(streams)
```
```{r}
is.character(artist_name)
```
```{r}
# What if a number is stored as text?
fake_number <- "42"
class(fake_number) # It's a character!
is.numeric(fake_number) # FALSE — R can't do math with it
```
```{r}
# Convert it to a real number
real_number <- as.numeric(fake_number)
is.numeric(real_number) # Now it's TRUE
```
------------------------------------------------------------------------
## Part 3: Working with Numbers
### Arithmetic Operators
```{r}
# Taylor Swift's Eras Tour had 152 shows in 2023-2024
# She played to an average of 72,000 fans per show
shows <- 152
avg_fans <- 72000
total_fans <- shows * avg_fans
total_fans
```
```{r}
# Format with commas using formatC (makes large numbers readable)
formatC(total_fans, format = "d", big.mark = ",")
```
::: callout-note
## R Arithmetic Operators
| Operator | Meaning | Example | Result |
|----------|---------|---------|--------|
| `+` | Addition | `5 + 3` | `8` |
| `-` | Subtraction | `10 - 4` | `6` |
| `*` | Multiplication | `6 * 7` | `42` |
| `/` | Division | `20 / 4` | `5` |
| `^` | Power | `2^10` | `1024` |
| `%%` | Remainder | `17 %% 5` | `2` |
:::
```{r}
# Each Eras Tour ticket averaged $254
ticket_price <- 254
revenue_estimate <- total_fans * ticket_price
revenue_estimate
# Convert to billions
revenue_estimate / 1000000000
```
------------------------------------------------------------------------
## Part 4: Working with Text (Characters)
### Useful Functions for Text
```{r}
album <- "The Tortured Poets Department"
# How many characters long is this album title?
nchar(album)
```
```{r}
# Convert to uppercase
toupper(album)
```
```{r}
# Convert to lowercase
tolower(album)
```
```{r}
# Combine text with paste()
artist <- "Taylor Swift"
year <- 2024
paste(artist, "released", album, "in", year)
```
```{r}
# paste0() combines with NO spaces
paste0("Track_", 1:5) # We'll learn about 1:5 properly in the next lab!
```
::: callout-tip
## `paste()` vs `paste0()`
- `paste()` puts a **space** between items by default
- `paste0()` puts **nothing** between items
You can also change the separator in `paste()`:
`paste("a", "b", "c", sep = "-")` gives `"a-b-c"`
:::
------------------------------------------------------------------------
## Part 5: Logical Values and Comparisons
Logical values (`TRUE` / `FALSE`) are the result of asking R a yes/no question. They are the foundation of filtering data — which you'll use constantly.
```{r}
# Comparison operators
midnights_sales <- 1600000 # first-week US sales
fearless_sales <- 592000 # first-week US sales (original release)
midnights_sales > fearless_sales # Is Midnights bigger?
```
```{r}
midnights_sales == fearless_sales # Are they equal? (double == for comparison!)
```
```{r}
midnights_sales != fearless_sales # Are they NOT equal?
```
::: callout-warning
## ️ `=` vs `==`
This is one of the most common beginner mistakes in R!
- `<-` or `=` — **assigns** a value to a variable
- `==` — **tests** whether two things are equal (asks a question)
`x <- 5` → puts 5 into x
`x == 5` → asks "is x equal to 5?" and returns `TRUE` or `FALSE`
:::
```{r}
# Combining logical conditions
grammy_wins <- 14
billboards_number_ones <- 40
# AND: both must be true
grammy_wins > 10 & billboards_number_ones > 30
```
```{r}
# OR: at least one must be true
grammy_wins > 20 | billboards_number_ones > 30
```
------------------------------------------------------------------------
## Part 6: Useful Built-in Functions
R comes with hundreds of built-in functions. Here are a few you'll use all the time:
```{r}
# Some of Taylor Swift's Spotify monthly listener counts (in millions) over 6 months
listeners <- c(95.2, 98.7, 102.1, 89.4, 110.3, 107.6)
# (Don't worry about c() yet — we'll cover it properly in Lab 4!)
# Summary statistics
sum(listeners) # Total
mean(listeners) # Average
min(listeners) # Lowest month
max(listeners) # Highest month
range(listeners) # Min and max together
length(listeners) # How many values
```
```{r}
# round() controls decimal places
mean_listeners <- mean(listeners)
round(mean_listeners, 1) # Round to 1 decimal place
```
::: callout-tip
## Getting Help in R
If you ever want to know what a function does or what arguments it takes, type `?` followed by the function name in the console:
```r
?round
?mean
?paste
```
A help page will appear in the bottom-right panel. The **Examples** section at the bottom is usually the most useful part.
:::
------------------------------------------------------------------------
## Part 7: Putting It All Together
Let's write a small summary of Taylor Swift's career using everything we've learned:
```{r}
# --- Taylor Swift Career Summary ---
artist_name <- "Taylor Swift"
debut_year <- 2006
current_year <- 2026
career_length <- current_year - debut_year
studio_albums <- 10
grammy_awards <- 14
eras_revenue <- 2077000000 # USD
# Print a summary
cat("Artist:", artist_name, "\n")
cat("Career length:", career_length, "years\n")
cat("Studio albums:", studio_albums, "\n")
cat("Grammy Awards:", grammy_awards, "\n")
cat("Eras Tour revenue: $", formatC(eras_revenue, format="d", big.mark=","), "\n")
cat("Is she a Grammy winner?", grammy_awards > 0, "\n")
```
::: callout-note
## What is `cat()`?
`cat()` ("concatenate and print") is used to print formatted output. `\n` means "new line." It's great for making readable summaries.
:::
------------------------------------------------------------------------
## 3-Minute Knowledge Check
*Close your notes. Answer these on your own — you have 3 minutes. We'll go through the answers together.*
::: callout-caution
## Knowledge Check Questions
**1.** What does `class("Fearless")` return?
a) `"numeric"` b) `"logical"` c) `"character"` d) `"text"`
**2.** What is the key difference between `=` and `==` in R?
**3.** Taylor Swift has 14 Grammy Awards. Which line of code correctly stores this?
a) `grammys == 14` b) `14 <- grammys` c) `grammys <- 14` d) `grammys = "14"`
**4.** What does `nchar("Midnights")` return?
a) `1` b) `9` c) `"Midnights"` d) `"character"`
**5.** True or False: `is.numeric("42")` returns `TRUE`.
:::
::: {.callout-caution collapse="true"}
## Answers (Only reveal if you have answered the above questions)
**1.** c) `"character"` — Any value in quotes is a character (text) type, regardless of what it says.
**2.** `=` (or `<-`) **assigns** a value to a variable. `==` **tests** whether two values are equal and returns `TRUE` or `FALSE`. This is one of the most common beginner mistakes.
**3.** c) `grammys <- 14` — Assignment goes left: the name on the left, the value on the right. Option d) is wrong because `"14"` is stored as text, not a number.
**4.** b) `9` — "Midnights" has 9 characters (M-i-d-n-i-g-h-t-s).
**5.** **False** — `"42"` is in quotes, so it is a **character**, not a numeric. `is.numeric("42")` returns `FALSE`. You would need `as.numeric("42")` to convert it first.
:::
------------------------------------------------------------------------
## Lab 2 Checklist
Before you leave, make sure you can:
- [ ] Create variables using `<-` and give them descriptive names
- [ ] Identify whether a value is `numeric`, `character`, or `logical`
- [ ] Use `class()` to check the type of a variable
- [ ] Do arithmetic with variables
- [ ] Use `paste()` to combine text
- [ ] Use comparison operators (`>`, `<`, `==`, `!=`) to get `TRUE`/`FALSE` results
- [ ] Use at least 3 of the summary functions: `sum()`, `mean()`, `min()`, `max()`, `length()`
::: callout-tip
## Bonus Challenge
Choose a different music artist. Create variables for their name, debut year, number of albums, and approximate total streams. Use `cat()` to print a formatted career summary. Add it to your Quarto document and render it!
:::
------------------------------------------------------------------------
## Before Next Class (Monday)
- Read **Intro2r §2.4.1–2.4.5, 3.1, 3.2, 7.2**
- Review your notes from today — especially the difference between `=` and `==`
- Remember: **Quiz 1 is this Friday** — it covers Labs 1 and 2
::: callout-important
## Quiz 1 Reminder
Quiz 1 is **in class this Friday (Aug 21)**. It will cover:
- Basic R syntax and operators
- Variable assignment
- Data types (numeric, character, logical)
- Simple functions (`class()`, `mean()`, `paste()`, etc.)
Review your notes and practice in the console!
:::
------------------------------------------------------------------------