Lab 2: Basic Building Blocks in R

BI 255 · Bethune-Cookman University · Fall 2026

Author

Dr. Rosie Stanbrook-Buyer

Published

August 19, 2026


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.

NoteLearning 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.

# 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
# 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
[1] 2077
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

# 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

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:

# Numeric
streams <- 1500000000
class(streams)
[1] "numeric"
# Character
artist_name <- "Taylor Swift"
class(artist_name)
[1] "character"
# Logical
is_grammy_winner <- TRUE
class(is_grammy_winner)
[1] "logical"
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

# is.numeric() asks "is this a number?" and returns TRUE or FALSE
is.numeric(streams)
[1] TRUE
is.character(artist_name)
[1] TRUE
# What if a number is stored as text?
fake_number <- "42"
class(fake_number)       # It's a character!
[1] "character"
is.numeric(fake_number)  # FALSE — R can't do math with it
[1] FALSE
# Convert it to a real number
real_number <- as.numeric(fake_number)
is.numeric(real_number)  # Now it's TRUE
[1] TRUE

Part 3: Working with Numbers

Arithmetic Operators

# 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
[1] 10944000
# Format with commas using formatC (makes large numbers readable)
formatC(total_fans, format = "d", big.mark = ",")
[1] "10,944,000"
NoteR 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
# Each Eras Tour ticket averaged $254
ticket_price <- 254
revenue_estimate <- total_fans * ticket_price
revenue_estimate
[1] 2779776000
# Convert to billions
revenue_estimate / 1000000000
[1] 2.779776

Part 4: Working with Text (Characters)

Useful Functions for Text

album <- "The Tortured Poets Department"

# How many characters long is this album title?
nchar(album)
[1] 29
# Convert to uppercase
toupper(album)
[1] "THE TORTURED POETS DEPARTMENT"
# Convert to lowercase
tolower(album)
[1] "the tortured poets department"
# Combine text with paste()
artist <- "Taylor Swift"
year   <- 2024

paste(artist, "released", album, "in", year)
[1] "Taylor Swift released The Tortured Poets Department in 2024"
# paste0() combines with NO spaces
paste0("Track_", 1:5)   # We'll learn about 1:5 properly in the next lab!
[1] "Track_1" "Track_2" "Track_3" "Track_4" "Track_5"
Tippaste() 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.

# 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?
[1] TRUE
midnights_sales == fearless_sales   # Are they equal? (double == for comparison!)
[1] FALSE
midnights_sales != fearless_sales   # Are they NOT equal?
[1] TRUE
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

# Combining logical conditions
grammy_wins <- 14
billboards_number_ones <- 40

# AND: both must be true
grammy_wins > 10 & billboards_number_ones > 30
[1] TRUE
# OR: at least one must be true
grammy_wins > 20 | billboards_number_ones > 30
[1] TRUE

Part 6: Useful Built-in Functions

R comes with hundreds of built-in functions. Here are a few you’ll use all the time:

# 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
[1] 603.3
mean(listeners)      # Average
[1] 100.55
min(listeners)       # Lowest month
[1] 89.4
max(listeners)       # Highest month
[1] 110.3
range(listeners)     # Min and max together
[1]  89.4 110.3
length(listeners)    # How many values
[1] 6
# round() controls decimal places
mean_listeners <- mean(listeners)
round(mean_listeners, 1)   # Round to 1 decimal place
[1] 100.6
TipGetting 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:

?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:

# --- 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")
Artist: Taylor Swift 
cat("Career length:", career_length, "years\n")
Career length: 20 years
cat("Studio albums:", studio_albums, "\n")
Studio albums: 10 
cat("Grammy Awards:", grammy_awards, "\n")
Grammy Awards: 14 
cat("Eras Tour revenue: $", formatC(eras_revenue, format="d", big.mark=","), "\n")
Eras Tour revenue: $ 2,077,000,000 
cat("Is she a Grammy winner?", grammy_awards > 0, "\n")
Is she a Grammy winner? TRUE 
NoteWhat 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.

CautionKnowledge Check Questions

1. What does class("Fearless") return?

a) `"numeric"`  &nbsp;&nbsp; b) `"logical"`  &nbsp;&nbsp; c) `"character"`  &nbsp;&nbsp; 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`  &nbsp;&nbsp; b) `14 <- grammys`  &nbsp;&nbsp; c) `grammys <- 14`  &nbsp;&nbsp; d) `grammys = "14"`

4. What does nchar("Midnights") return?

a) `1`  &nbsp;&nbsp; b) `9`  &nbsp;&nbsp; c) `"Midnights"`  &nbsp;&nbsp; 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.


Lab 2 Checklist

Before you leave, make sure you can:

TipBonus 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
ImportantQuiz 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!