library(readr)
pixar_films <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-03-11/pixar_films.csv")
## Rows: 27 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): film, film_rating
## dbl (2): number, run_time
## date (1): release_date
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Transform data: calculate average run time per film rating
runtime_by_rating <- pixar_films %>%
group_by(film_rating) %>%
summarize(
avg_runtime = mean(run_time, na.rm = TRUE)
)
runtime_by_rating
## # A tibble: 4 × 2
## film_rating avg_runtime
## <chr> <dbl>
## 1 G 100.
## 2 N/A 151
## 3 Not Rated 155
## 4 PG 101.
# Plot
runtime_by_rating %>%
ggplot(aes(x = avg_runtime, y = film_rating)) +
geom_point()
runtime_by_rating <- pixar_films %>%
group_by(film_rating) %>%
summarize(
avg_runtime = mean(run_time, na.rm = TRUE)
)
runtime_by_rating
## # A tibble: 4 × 2
## film_rating avg_runtime
## <chr> <dbl>
## 1 G 100.
## 2 N/A 151
## 3 Not Rated 155
## 4 PG 101.
# Plot
runtime_by_rating %>%
ggplot(aes(x = avg_runtime, y = fct_reorder(.f = film_rating, .x = avg_runtime))) +
geom_point() +
#labeling
labs(y = NULL, x = "Mean Run Time (Minutes)")
pixar_films %>% distinct(film_rating)
## # A tibble: 4 × 1
## film_rating
## <chr>
## 1 G
## 2 PG
## 3 N/A
## 4 Not Rated
# Recode
pixar_films %>%
#Rename Levels
mutate(rating_rev = fct_recode(film_rating, "General" = "G", "Parental Guidance" = "PG")) %>%
select(film_rating, rating_rev) %>%
filter(film_rating == "G")
## # A tibble: 13 × 2
## film_rating rating_rev
## <chr> <fct>
## 1 G General
## 2 G General
## 3 G General
## 4 G General
## 5 G General
## 6 G General
## 7 G General
## 8 G General
## 9 G General
## 10 G General
## 11 G General
## 12 G General
## 13 G General
# Collapse multiple levels into one
pixar_films %>%
mutate(rating_col = fct_collapse(film_rating, "PG & Above" = c("PG", "PG-13"))) %>%
select(film_rating, rating_col) %>%
filter(film_rating != "G")
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `rating_col = fct_collapse(film_rating, `PG & Above` = c("PG",
## "PG-13"))`.
## Caused by warning:
## ! Unknown levels in `f`: PG-13
## # A tibble: 14 × 2
## film_rating rating_col
## <chr> <fct>
## 1 PG PG & Above
## 2 PG PG & Above
## 3 PG PG & Above
## 4 PG PG & Above
## 5 PG PG & Above
## 6 PG PG & Above
## 7 PG PG & Above
## 8 PG PG & Above
## 9 PG PG & Above
## 10 PG PG & Above
## 11 N/A N/A
## 12 N/A N/A
## 13 N/A N/A
## 14 Not Rated Not Rated
# Lump small levels into other level
pixar_films %>% count(film_rating)
## # A tibble: 4 × 2
## film_rating n
## <chr> <int>
## 1 G 13
## 2 N/A 3
## 3 Not Rated 1
## 4 PG 10
pixar_films %>% mutate(rating_lump = fct_lump(film_rating)) %>% distinct(rating_lump)
## # A tibble: 3 × 1
## rating_lump
## <fct>
## 1 G
## 2 PG
## 3 Other
No need to do anything here.