# 1. Rename Columns
renamed_movies <- movies %>%
  rename(
    movie_title = Film  ,       # Rename 'movie_title' to 'Film' 
    release_year  = Year     # Rename 'release_year' to 'Year'
  )

head(renamed_movies)
## # 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>
#2 Select Columns
movies_selected <- renamed_movies %>%
  select(movie_title, release_year, Genre, Profitability, `Rotten Tomatoes %`)

# Print the first 6 rows of the selected dataset
head(movies_selected)
## # 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
#3 Filter Columns
movies_filtered <- renamed_movies %>%
  filter(release_year > 2000, `Rotten Tomatoes %` > 80)

# View the first 6 rows of the filtered dataset
head(movies_filtered)
## # A tibble: 6 × 8
##   movie_title            Genre    `Lead Studio` `Audience score %` Profitability
##   <chr>                  <chr>    <chr>                      <dbl>         <dbl>
## 1 WALL-E                 Animati… Disney                        89         2.90 
## 2 Waitress               Romance  Independent                   67        11.1  
## 3 Tangled                Animati… Disney                        88         1.37 
## 4 Rachel Getting Married Drama    Independent                   61         1.38 
## 5 My Week with Marilyn   Drama    The Weinstei…                 84         0.826
## 6 Midnight in Paris      Romence  Sony                          84         8.74 
## # ℹ 3 more variables: `Rotten Tomatoes %` <dbl>, `Worldwide Gross` <chr>,
## #   release_year <dbl>
#4 Mutate to Add New Column
movies_filtered <- movies_filtered %>%
  mutate(Profitability_millions = Profitability / 1e6)

# Print the first 6 rows of the dataset with the new column
head(movies_filtered)
## # A tibble: 6 × 9
##   movie_title            Genre    `Lead Studio` `Audience score %` Profitability
##   <chr>                  <chr>    <chr>                      <dbl>         <dbl>
## 1 WALL-E                 Animati… Disney                        89         2.90 
## 2 Waitress               Romance  Independent                   67        11.1  
## 3 Tangled                Animati… Disney                        88         1.37 
## 4 Rachel Getting Married Drama    Independent                   61         1.38 
## 5 My Week with Marilyn   Drama    The Weinstei…                 84         0.826
## 6 Midnight in Paris      Romence  Sony                          84         8.74 
## # ℹ 4 more variables: `Rotten Tomatoes %` <dbl>, `Worldwide Gross` <chr>,
## #   release_year <dbl>, Profitability_millions <dbl>
#5 Arrange Dataset
movies_sorted <- movies_filtered %>%
  arrange(desc(`Rotten Tomatoes %`), desc(Profitability_millions))

# Print the first 6 rows of the sorted dataset
head(movies_sorted)
## # A tibble: 6 × 9
##   movie_title       Genre     `Lead Studio` `Audience score %` Profitability
##   <chr>             <chr>     <chr>                      <dbl>         <dbl>
## 1 WALL-E            Animation Disney                        89          2.90
## 2 Midnight in Paris Romence   Sony                          84          8.74
## 3 Enchanted         Comedy    Disney                        80          4.01
## 4 Knocked Up        Comedy    Universal                     83          6.64
## 5 Waitress          Romance   Independent                   67         11.1 
## 6 A Serious Man     Drama     Universal                     64          4.38
## # ℹ 4 more variables: `Rotten Tomatoes %` <dbl>, `Worldwide Gross` <chr>,
## #   release_year <dbl>, Profitability_millions <dbl>
#6 Combine Functions
final_df <- 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 / 1e6) %>%
  arrange(desc(`Rotten Tomatoes %`), desc(Profitability_millions))

# Print the first 6 rows of the final combined dataset
head(final_df)
## # 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>
print(names(movies))
## [1] "Film"              "Genre"             "Lead Studio"      
## [4] "Audience score %"  "Profitability"     "Rotten Tomatoes %"
## [7] "Worldwide Gross"   "Year"

#7 Interpret the Final Dataframe Interpretation: The data shows a trend that higher Rotten Tomatoes percentages often correlate with higher profitability; however, there are exceptions where some less profitable films receive high ratings. This indicates that while critical acclaim may contribute to popularity, it does not always guarantee box office success

#8 Extra Credit 
summary_df <- movies %>%
  rename(
    movie_title = Film,
    release_year = Year
  ) %>%
  group_by(Genre) %>%
  summarize(
    average_rating = mean(`Rotten Tomatoes %`, na.rm = TRUE),
    average_profitability_millions = mean(Profitability / 1e6, na.rm = TRUE)
  )

# Print the summary dataframe
print(summary_df)
## # A tibble: 10 × 3
##    Genre     average_rating average_profitability_millions
##    <chr>              <dbl>                          <dbl>
##  1 Action              11                      0.00000125 
##  2 Animation           74.2                    0.00000376 
##  3 Comdy               13                      0.00000265 
##  4 Comedy              42.7                    0.00000378 
##  5 Drama               51.5                    0.00000841 
##  6 Fantasy             73                      0.00000178 
##  7 Romance             42.1                    0.00000398 
##  8 Romence             93                      0.00000874 
##  9 comedy              87                      0.00000810 
## 10 romance             54                      0.000000653