Week3_A

Author

Reginald Dorcely

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(stringr)
library(lubridate)

# Movie ratings dataset
ratings <- tibble(
  person = c(
    "Alice", "Alice", "Alice", "Alice", "Alice",
    "Brian", "Brian", "Brian", "Brian", "Brian"
  ),
  
  movie = c(
    "Spider-Man: Brand New Day",
    "The Odyssey",
    "Toy Story 5",
    "The Super Mario Galaxy Movie",
    "Project Hail Mary",
    "Spider-Man: Brand New Day",
    "The Odyssey",
    "Toy Story 5",
    "The Super Mario Galaxy Movie",
    "Project Hail Mary"
  ),
  
  rating = c(
    5, 4, 4, 5, 4,
    4, 5, 3, 4, 5
  )
)

# ------------------------------
# Work with dates
# ------------------------------

ratings <- ratings %>%
  mutate(
    rating_date = seq.Date(
      from = as.Date("2026-09-01"),
      by = "day",
      length.out = n()
    ),
    
    year = year(rating_date),
    month = month(rating_date, label = TRUE),
    day = day(rating_date),
    weekday = wday(rating_date, label = TRUE)
  )

# ------------------------------
# Work with strings
# ------------------------------

ratings <- ratings %>%
  mutate(
    person_upper = str_to_upper(person),
    
    movie_upper = str_to_upper(movie),
    
    title_length = str_length(movie),
    
    movie_clean = str_squish(movie)
  )

# ------------------------------
# Use regular expressions
# ------------------------------

ratings <- ratings %>%
  mutate(
    # Detect a number
    contains_number =
      str_detect(movie, "\\d"),
    
    # Extract a number
    movie_number =
      str_extract(movie, "\\d+"),
    
    # Detect movies beginning with "The"
    starts_with_the =
      str_detect(movie, "^The"),
    
    # Detect movies ending with "Movie"
    ends_with_movie =
      str_detect(movie, "Movie$")
  )

# View results
ratings
# A tibble: 10 × 16
   person movie        rating rating_date  year month   day weekday person_upper
   <chr>  <chr>         <dbl> <date>      <dbl> <ord> <int> <ord>   <chr>       
 1 Alice  Spider-Man:…      5 2026-09-01   2026 Sep       1 Tue     ALICE       
 2 Alice  The Odyssey       4 2026-09-02   2026 Sep       2 Wed     ALICE       
 3 Alice  Toy Story 5       4 2026-09-03   2026 Sep       3 Thu     ALICE       
 4 Alice  The Super M…      5 2026-09-04   2026 Sep       4 Fri     ALICE       
 5 Alice  Project Hai…      4 2026-09-05   2026 Sep       5 Sat     ALICE       
 6 Brian  Spider-Man:…      4 2026-09-06   2026 Sep       6 Sun     BRIAN       
 7 Brian  The Odyssey       5 2026-09-07   2026 Sep       7 Mon     BRIAN       
 8 Brian  Toy Story 5       3 2026-09-08   2026 Sep       8 Tue     BRIAN       
 9 Brian  The Super M…      4 2026-09-09   2026 Sep       9 Wed     BRIAN       
10 Brian  Project Hai…      5 2026-09-10   2026 Sep      10 Thu     BRIAN       
# ℹ 7 more variables: movie_upper <chr>, title_length <int>, movie_clean <chr>,
#   contains_number <lgl>, movie_number <chr>, starts_with_the <lgl>,
#   ends_with_movie <lgl>

You can add options to executable code like this

library(tidyverse)
library(stringr)
library(lubridate)

# Create movie-rating dataset
ratings <- tibble(
  person = rep(
    c("Alice", "Brian", "Carla", "David",
      "Elena", "Frank", "Grace"),
    each = 5
  ),
  
  movie = rep(
    c(
      "Spider-Man: Brand New Day",
      "The Odyssey",
      "Toy Story 5",
      "The Super Mario Galaxy Movie",
      "Project Hail Mary"
    ),
    times = 7
  ),
  
  rating = c(
    5,4,4,5,4,
    4,5,3,4,5,
    5,4,5,4,4,
    3,5,4,5,5,
    4,4,5,4,3,
    5,3,4,5,4,
    4,5,4,3,5
  )
)

# Work with dates
ratings <- ratings %>%
  mutate(
    rating_date = seq.Date(
      from = as.Date("2026-08-01"),
      by = "day",
      length.out = n()
    ),
    year = year(rating_date),
    month = month(rating_date, label = TRUE),
    weekday = wday(rating_date, label = TRUE)
  )

# Work with strings and regular expressions
ratings <- ratings %>%
  mutate(
    person_upper = str_to_upper(person),
    movie_clean = str_squish(movie),
    title_length = str_length(movie),
    has_number = str_detect(movie, "\\d"),
    movie_number = str_extract(movie, "\\d+")
  )

# Calculate global baseline estimate
global_baseline <- mean(ratings$rating, na.rm = TRUE)

# Add baseline and residuals
ratings <- ratings %>%
  mutate(
    global_baseline = global_baseline,
    residual = rating - global_baseline
  )

# Average rating by movie
movie_summary <- ratings %>%
  group_by(movie) %>%
  summarise(
    n = n(),
    average_rating = mean(rating),
    .groups = "drop"
  )

# Display results
round(global_baseline, 2)
[1] 4.26
ratings
# A tibble: 35 × 14
   person movie  rating rating_date  year month weekday person_upper movie_clean
   <chr>  <chr>   <dbl> <date>      <dbl> <ord> <ord>   <chr>        <chr>      
 1 Alice  Spide…      5 2026-08-01   2026 Aug   Sat     ALICE        Spider-Man…
 2 Alice  The O…      4 2026-08-02   2026 Aug   Sun     ALICE        The Odyssey
 3 Alice  Toy S…      4 2026-08-03   2026 Aug   Mon     ALICE        Toy Story 5
 4 Alice  The S…      5 2026-08-04   2026 Aug   Tue     ALICE        The Super …
 5 Alice  Proje…      4 2026-08-05   2026 Aug   Wed     ALICE        Project Ha…
 6 Brian  Spide…      4 2026-08-06   2026 Aug   Thu     BRIAN        Spider-Man…
 7 Brian  The O…      5 2026-08-07   2026 Aug   Fri     BRIAN        The Odyssey
 8 Brian  Toy S…      3 2026-08-08   2026 Aug   Sat     BRIAN        Toy Story 5
 9 Brian  The S…      4 2026-08-09   2026 Aug   Sun     BRIAN        The Super …
10 Brian  Proje…      5 2026-08-10   2026 Aug   Mon     BRIAN        Project Ha…
# ℹ 25 more rows
# ℹ 5 more variables: title_length <int>, has_number <lgl>, movie_number <chr>,
#   global_baseline <dbl>, residual <dbl>
movie_summary
# A tibble: 5 × 3
  movie                            n average_rating
  <chr>                        <int>          <dbl>
1 Project Hail Mary                7           4.29
2 Spider-Man: Brand New Day        7           4.29
3 The Odyssey                      7           4.29
4 The Super Mario Galaxy Movie     7           4.29
5 Toy Story 5                      7           4.14

The echo: false option disables the printing of code (only output is displayed).