Rename the “Film” column to “movie_title” and “Year” to “release_year”
question1 <- movies %>%
rename(movie_title = Film , release_year = Year)
head(question1)
## # A tibble: 6 × 8
## movie_title Genre `Lead Studio` `Audience score %` Profitability
## <chr> <chr> <chr> <dbl> <dbl>
## 1 Zack and Miri Make a Por… Roma… The Weinstei… 70 1.75
## 2 Youth in Revolt Come… The Weinstei… 52 1.09
## 3 You Will Meet a Tall Dar… Come… Independent 35 1.21
## 4 When in Rome Come… Disney 44 0
## 5 What Happens in Vegas Come… Fox 72 6.27
## 6 Water For Elephants Drama 20th Century… 72 3.08
## # ℹ 3 more variables: `Rotten Tomatoes %` <dbl>, `Worldwide Gross` <chr>,
## # release_year <dbl>
Create a new dataframe with only the columns: movie_title, release_year, Genre, Profitability,
question2 <- question1 %>%
select(movie_title, release_year, Genre, Profitability, 'Rotten Tomatoes %')
head(question2)
## # A tibble: 6 × 5
## movie_title release_year Genre Profitability `Rotten Tomatoes %`
## <chr> <dbl> <chr> <dbl> <dbl>
## 1 Zack and Miri Make a Por… 2008 Roma… 1.75 64
## 2 Youth in Revolt 2010 Come… 1.09 68
## 3 You Will Meet a Tall Dar… 2010 Come… 1.21 43
## 4 When in Rome 2010 Come… 0 15
## 5 What Happens in Vegas 2008 Come… 6.27 28
## 6 Water For Elephants 2011 Drama 3.08 60
Filter the dataset to include only movies released after 2000 with a Rotten Tomatoes % higher than 80.
question3 <- question2 %>%
filter(release_year > 2000, 'Rotten Tomatoes %' > 80)
head(question3)
## # A tibble: 6 × 5
## movie_title release_year Genre Profitability `Rotten Tomatoes %`
## <chr> <dbl> <chr> <dbl> <dbl>
## 1 Zack and Miri Make a Por… 2008 Roma… 1.75 64
## 2 Youth in Revolt 2010 Come… 1.09 68
## 3 You Will Meet a Tall Dar… 2010 Come… 1.21 43
## 4 When in Rome 2010 Come… 0 15
## 5 What Happens in Vegas 2008 Come… 6.27 28
## 6 Water For Elephants 2011 Drama 3.08 60
Add a new column called “Profitability_millions” that converts Profitability to millions of dollars.
question4 <- question3 %>%
mutate(Profitability_millions = Profitability / 1000000)
head(question4)
## # A tibble: 6 × 6
## movie_title release_year Genre Profitability `Rotten Tomatoes %`
## <chr> <dbl> <chr> <dbl> <dbl>
## 1 Zack and Miri Make a Por… 2008 Roma… 1.75 64
## 2 Youth in Revolt 2010 Come… 1.09 68
## 3 You Will Meet a Tall Dar… 2010 Come… 1.21 43
## 4 When in Rome 2010 Come… 0 15
## 5 What Happens in Vegas 2008 Come… 6.27 28
## 6 Water For Elephants 2011 Drama 3.08 60
## # ℹ 1 more variable: Profitability_millions <dbl>
Sort the filtered dataset by Rotten Tomatoes % in descending order, then by Profitability_millions in descending order.
question5 <- question4 %>%
arrange(desc('Rotten Tomatoes %'), desc(Profitability_millions))
head(question5)
## # A tibble: 6 × 6
## movie_title release_year Genre Profitability `Rotten Tomatoes %`
## <chr> <dbl> <chr> <dbl> <dbl>
## 1 Fireproof 2008 Drama 66.9 40
## 2 High School Musical 3: S… 2008 Come… 22.9 65
## 3 The Twilight Saga: New M… 2009 Drama 14.2 27
## 4 Waitress 2007 Roma… 11.1 89
## 5 Twilight 2008 Roma… 10.2 49
## 6 Mamma Mia! 2008 Come… 9.23 53
## # ℹ 1 more variable: Profitability_millions <dbl>
Use the pipe operator (%>%) to chain these operations together, starting with the original dataset and ending with a final dataframe that incorporates all the above transformations.
final <- movies %>%
rename(movie_title = Film, release_year = Year) %>%
select(movie_title, release_year, Genre, Profitability, `Rotten Tomatoes %`) %>%
filter(release_year > 2000, `Rotten Tomatoes %` > 80) %>%
mutate(Profitability_millions = Profitability / 1000000) %>%
arrange(desc(`Rotten Tomatoes %`), desc(Profitability_millions))
head(final)
## # A tibble: 6 × 6
## movie_title release_year Genre Profitability `Rotten Tomatoes %`
## <chr> <dbl> <chr> <dbl> <dbl>
## 1 WALL-E 2008 Animation 2.90 96
## 2 Midnight in Paris 2011 Romence 8.74 93
## 3 Enchanted 2007 Comedy 4.01 93
## 4 Knocked Up 2007 Comedy 6.64 91
## 5 Waitress 2007 Romance 11.1 89
## 6 A Serious Man 2009 Drama 4.38 89
## # ℹ 1 more variable: Profitability_millions <dbl>
From the resulting data, are the best movies the most popular?
No, the best movies are not always the most popular.
Create a summary dataframe that shows the average rating and Profitability_millions for movies by Genre.
summary_by_genre <- movies %>%
mutate(Profitability_millions = Profitability * 1000000) %>%
mutate(Genre = recode(Genre,
"Comdy" = "Comedy",
"comedy" = "Comedy",
"Romence" = "Romance",
"romance" = "Romance")) %>%
group_by(Genre) %>%
summarize(
avg_rating = mean(`Rotten Tomatoes %`, na.rm = TRUE),
avg_profitability_millions = mean(Profitability_millions, na.rm = TRUE)
)
head(summary_by_genre)
## # A tibble: 6 × 3
## Genre avg_rating avg_profitability_millions
## <chr> <dbl> <dbl>
## 1 Action 11 1245333.
## 2 Animation 74.2 3759414.
## 3 Comedy 43.0 3851160.
## 4 Drama 51.5 8407218.
## 5 Fantasy 73 1783944.
## 6 Romance 46.3 4079972.