moderndive / ModernDive_book

Statistical Inference via Data Science: A ModernDive into R and the Tidyverse
https://www.moderndive.com/
Other
759 stars 491 forks source link

Correction to Solution LC 9.15 #447

Closed rudeboybert closed 2 years ago

rudeboybert commented 2 years ago

From Don H. by email (LC9.15) Test your data wrangling knowledge and EDA skills:

Use dplyr and tidyr to create the necessary data frame focused on only action and romance movies (but not both) from the movies data frame in the ggplot2movies package. Make a boxplot and a faceted histogram of this population data comparing ratings of action and romance movies from IMDb. Discuss how these plots compare to the similar plots produced for the movies_sample data.

Solution:

Use dplyr and tidyr to create the necessary data frame focused on only action and romance movies (but not both) from the movies data frame in the ggplot2movies package.

action_romance <- movies %>%
  filter(Action == 1 & Romance == 1)

My solution:

action_romance <- movies %>%
  select(title, year, rating, Action, Romance) %>%
  filter((Action == 1 | Romance == 1) & 
           !(Action == 1 & Romance == 1)) %>%
  pivot_longer(names_to = 'genre',
               cols = c(Action, Romance)) %>%
  filter(value == 1) %>%
  select(-value)
action_romance