Data: Hollywood movies
HollywoodMovies dataset.WorldwideBO?WorldwideBO in each genre?Tidyverse functions take a data frame as their first input and return a data frame, so functions can be chained with the native R pipe |> or tidyverse pipe %>%.
Read the pipe as “then”: start with HollywoodMovies, then keep action movies, then select the three named columns.
The square bracket syntax can be used as a function, with the function name ‘[’
(Use ?dplyr::filter to read the help page)
slice_sample()arrange() and slice_max()# Smallest five worldwide grosses
HollywoodMovies |>
arrange(WorldwideBO) |>
slice_head(n = 5)
# Largest five worldwide grosses
HollywoodMovies |>
slice_max(order_by = WorldwideBO, n = 5, na_rm = TRUE) |>
select(Movie, Genre, WorldwideBO)
HollywoodMovies |>
arrange(desc(WorldwideBO)) |>
slice(1:5)
# Top three values of a derived quantity; ties are retained by default
HollywoodMovies |>
mutate(score_difference = abs(RottenTomatoes - AudienceScore)) |>
slice_max(order_by = score_difference, n = 3)If you are only interested in action movies, say, you might use filter().
If you now want to generalize to other genres, rather than manually changing “Action” to other genres, you can group by genre instead.
The .groups = "drop" argument ensures that the resulting data frame is no longer grouped by genre, so that later operations are not affected.
Can group by multiple variables.
# A tibble: 5 × 3
Genre mean_profit movie_count
<chr> <dbl> <int>
1 Concert/Performance 1743. 1
2 Horror 812. 59
3 Black Comedy 359. 6
4 Romantic Comedy 354. 7
5 Drama 325. 75
Note: nothing is stored; no change is made in the data frame.
# Create a derived column and select the five largest values
HollywoodMovies |>
mutate(score_gap = abs(RottenTomatoes - AudienceScore)) |>
slice_max(order_by = score_gap, n = 5) |>
select(Movie, RottenTomatoes, AudienceScore, score_gap)
# Save the updated data frame using explicit assignment
movies_with_gap <- HollywoodMovies |>
mutate(score_gap = abs(RottenTomatoes - AudienceScore))
# Keep only selected columns while creating a derived column
HollywoodMovies |>
transmute(Movie, score_gap = abs(RottenTomatoes - AudienceScore))
# Rename existing columns
HollywoodMovies |>
rename(open_weekend_gross = OpenBO, theaters_at_open = TheatersOpen)A tibble is a modern version of an R data frame. It prints a compact preview and preserves familiar data-frame behavior while integrating with tidyverse functions.
How many genres are there, and how many movies are in each genre?
What are the 10 movies with the largest absolute difference between RottenTomatoes and AudienceScore?
# slice_max() approach
HollywoodMovies |>
mutate(score_gap = abs(RottenTomatoes - AudienceScore)) |>
slice_max(order_by = score_gap, n = 10) |>
select(Movie, RottenTomatoes, AudienceScore, score_gap)
# Alternative using arrange() and slice()
HollywoodMovies |>
mutate(score_gap = abs(RottenTomatoes - AudienceScore)) |>
arrange(desc(score_gap)) |>
slice(1:10) |>
select(Movie, RottenTomatoes, AudienceScore, score_gap)Compare the mean AudienceScore by genre. This uses the grouped summary method introduced above.
What are the five action movies with the largest WorldwideBO?
# slice_max() approach
HollywoodMovies |>
filter(Genre == "Action") |>
slice_max(order_by = WorldwideBO, n = 5) |>
select(Movie, Genre, WorldwideBO)
# Alternative using arrange() and slice()
HollywoodMovies |>
filter(Genre == "Action") |>
arrange(desc(WorldwideBO)) |>
slice(1:5) |>
select(Movie, Genre, WorldwideBO)Which movie has the largest WorldwideBO in each genre? slice_max() retains ties.