OSSA Assignment 2: Quarto Webpage

Author

Kasey Boyd

Show the set up code
library(dplyr)
library(forcats)
library(stringr)
Show the set up code
m <- read.csv("matts_movies.csv", stringsAsFactors=TRUE)
tCnt <- nrow(m)

T1.

How many genres are differentiated in the dataset?

16

This was obtained by taking the genre column and using the levels() function to determine the unique categories within the column. Then, the length() function was applied to count the amount of categories that we pulled out.

length(levels(m$Genre))
[1] 16

T2.

Which genre has the most entries?

Drama

This was obtained by grouping the movies by genre and then counting the amount of entries for each genre. The data was then arranged in descending order to see the genre with the highest amount of entries on top.

m |> 
  group_by(Genre) |> 
  count() |> 
  arrange(desc(n))
# A tibble: 16 × 2
# Groups:   Genre [16]
   Genre           n
   <fct>       <int>
 1 Drama         321
 2 Comedy        276
 3 Action        197
 4 Independent   190
 5 Thriller      165
 6 Foreign       157
 7 Horror         96
 8 Romance        94
 9 Family         84
10 Documentary    78
11 Sci-Fi         62
12 Classic        47
13 War            27
14 Sports         23
15 Fantasy        21
16 Western        14

T3.

What percentage of the movies did Matt rate above 8.5?

11.6%

To get this percentage, a filter was created to hone in on the ratings category, and pick out only the movies(rows) that had a rating of over 8.5. Then the movies that met the criteria were divided by the total amount of movies and then multiplied by 100 in order to get a percentage.

m85 <- m |> 
  filter(My.Rating > 8.5)

((nrow(m85))/(tCnt))*100
[1] 11.60907

T4.

What percentage of the movies are dramas?

17.3%

This process used a similar structure to the previous question, however instead of filtering by rating, the movies were filtered by genre, only selecting movies that were entered as a Drama. The same equation was applied from the last question (switching out the variable for the new mDrama) in order to calculate the percentage.

mDrama <- m |> 
  filter(Genre == "Drama")

((nrow(mDrama))/(tCnt))*100
[1] 17.33261

T5.

What percentage of the movies are dramas with a rating higher than 9.5?

0.27%

This problem required similar filters as before, but this time were piped into each other in order to get a filter within a filter. This allowed us to look at all of the movies entered in the Drama genre, and then from there look at the ratings column to filter down to those movies in which Matt rated over a 9.5. After this, the percentage was calculated using the same formula of dividing the movies left from the filter by the total number of movies, and then multiplying by 100.

mDrama95 <- m |>
  filter(Genre == "Drama") |>
  filter(My.Rating > 9.5) 
  
((nrow(mDrama95))/(tCnt))*100
[1] 0.2699784

T6.

How many movies were directed by Wes Anderson?

8

In this problem, the first step was to group the the movies(rows) by director (common values) and then filter the column to show only the groups that matched the value of “Wes Anderson”, and then finished by counting the number of entries in that group.

m |>
  group_by(Director) |>
  filter(Director == "Wes Anderson") |>
  count()
# A tibble: 1 × 2
# Groups:   Director [1]
  Director         n
  <fct>        <int>
1 Wes Anderson     8

T7.

What percentage of the movies were made after 2010, are comedies, and have a rating above 9?

0.16%

For this question, I created the mT7 function that had a filter on the release year being greater than 2010 and then piped that down to a filter for he genre to only include movies that have the value of “Comedy” fo the genre column, and then piped that into another filter to take the remaining movies from the first two filters and filter out any that do not have a rating above a 9. The last step was to find the percentage of the overall movies that fit this criteria by dividing the number left by the total amount of movies and then multiplying it by 100.

mT7 <- m |> 
  filter(Release.Year > 2010) |>
  filter(Genre == "Comedy") |>
  filter(My.Rating > 9) 

((nrow(mT7))/(tCnt))*100
[1] 0.161987

T8.

How many romance or independent films were released during or after 2005 and have a rating above 9?

10

For this problem, the same approach was taken in creating three filters to just keep narrowing down the selection based on the criteria. However, in this instance when creating the genre filter we needed to ensure that we created the option of either romance or independent genres, which we did by using the or operator “|” to separate the two genre options within the filter. For the release year a greater than or equals to operator was needed to include the movies released in 2005 and not just after. After the final rating filter was applied we then just took the count of the remaining movies as the answer.

mT8 <- m |> 
  filter(Genre == "Romance" | Genre == "Independent") |>
  filter(Release.Year >= 2005) |>
  filter(My.Rating > 9)

nrow(mT8)
[1] 10

T9.

What percentage of the independent films have a rating greater than 8.5?

1.08%

This problem followed a similar process in which we created a function of mT9 and then applied filters to narrow down the movies to those that fit the criteria. The genre filter included only those that fit the criteria of independent and then from that only those that had a rating above an 8.5 using another filter. After the filters were applied, the function was then called in order to calculate the percentage by dividing that number by the total number and then multiplying by 100.

mT9 <- m |>
  filter(Genre == "Independent") |>
  filter(My.Rating > 8.5)

((nrow(mT9))/(tCnt))*100
[1] 1.079914

T10.

What percentage of the movies on the list does Matt own?

11.55%

In this question a function mOwn was created to filter down the movies by piping down to a filter for the own column and then only taking movies that were equal to “Yes”. After this, the mOwn was applied in the function to take the percentage of these movies from the total.

mOwn <- m |>
  filter(Own == "Yes")

((nrow(mOwn))/(tCnt))*100
[1] 11.55508

T11.

Generate code to subset out only the “Movie.Name” and “Director” columns.

In order to subset out two columns we needed to use the select function to actually pull the columns out and separate them from the rest of the data.

m11 <- m |> select(Movie.Name, Director)

T12.

Generate code to obtain a data frame of the mean rating by genre.

In this problem we had to first subset out the two columns of genre and rating that we then could group by genre to create a new data frame with genre as the independent variable (x axis) and the rating column as the dependent variable (y axis). In this new data frame, we were then able to summarize the ratings in order to get the mean rating for each genre.

m |> select(Genre, My.Rating) |>
  group_by(Genre) |>
  summarize(mnR = mean(My.Rating))
# A tibble: 16 × 2
   Genre         mnR
   <fct>       <dbl>
 1 Action       6.65
 2 Classic      6.74
 3 Comedy       6.74
 4 Documentary  7.28
 5 Drama        7.14
 6 Family       6.67
 7 Fantasy      7.47
 8 Foreign      7.45
 9 Horror       6.52
10 Independent  6.94
11 Romance      7.24
12 Sci-Fi       6.87
13 Sports       6.39
14 Thriller     7.09
15 War          6.54
16 Western      6.96

T13.

Which genre has the highest mean rating?

Fantasy

This problem used the code created in the previous question as its foundation and then just sorted it in order to show the highest mean rating. This was done by adding in the line that used the arrange function and then specifying descending order for the mean rating using the mnR variable that was created in the summarize in order to.

m |> select(Genre, My.Rating) |>
  group_by(Genre) |>
  summarize(mnR = mean(My.Rating)) |>
  arrange(desc(mnR))
# A tibble: 16 × 2
   Genre         mnR
   <fct>       <dbl>
 1 Fantasy      7.47
 2 Foreign      7.45
 3 Documentary  7.28
 4 Romance      7.24
 5 Drama        7.14
 6 Thriller     7.09
 7 Western      6.96
 8 Independent  6.94
 9 Sci-Fi       6.87
10 Classic      6.74
11 Comedy       6.74
12 Family       6.67
13 Action       6.65
14 War          6.54
15 Horror       6.52
16 Sports       6.39

T14.

Which director, Wes Anderson or David Fincher, has the highest average rating for his films included in

the data set?

Wes Anderson

The first step in this problem is to subset out the director and rating columns using the select function and then using a pipe to the create a filter for the director to equal “wes Anderson” or (indicated by the pike symbol) “David Fincher”. After this, another pipe was added to organize this data into groups by director much like in the previous question. The last step was to summarize this new data frame by taking the mean of the ratings to then be able to see who had the higher average mean.

m |> select(Director, My.Rating) |>
  filter(Director == "Wes Anderson" | Director == "David Fincher") |>
  group_by(Director) |>
  summarize(mnR = mean(My.Rating))
# A tibble: 2 × 2
  Director        mnR
  <fct>         <dbl>
1 David Fincher  8.22
2 Wes Anderson   9.35

T15.

Generate code to randomly sample 25 movies from the data set without replacement.

In this problem the sample_n function was used, and then specify the sample number to be 25 as well as the replacement needing to be equal to false in order to not replacing.

m15 <- m |> sample_n(25, replace=FALSE)

T16.

Generate code to sample 1 movie per Genre without replacement.

This problem first grouped by genre and then used sample_n function and then specify a sample size of 1 and replacement to be false.

m16 <- m |> 
  group_by(Genre) |> 
  sample_n(1, replace=FALSE)

T17.

Which director has the largest range of ratings?

M. Night Shyamalan

This problem required a similar process as before with sub setting out the director and rating columns before grouping by director. After these lines the summarize function was applied to create a rng variable that was equal to the min of the ratings subratracted from the max of the ratings in order to get the range. Then the arrange function was applied to the rng that was created in descending order to see the director with the largest range of ratings.

 m |> select(Director, My.Rating) |>
  group_by(Director) |>
  summarize(rng = max(My.Rating)-min(My.Rating)) |>
  arrange(desc(rng))
# A tibble: 801 × 2
   Director             rng
   <fct>              <dbl>
 1 M. Night Shyamalan  9.01
 2 Gore Verbinski      7.63
 3 Clint Eastwood      7.55
 4 Doug Liman          7.33
 5 Lawrence Kasdan     7.28
 6 Bob Clark           7.21
 7 David O. Russell    7.14
 8 Gus Van Sant        6.97
 9 Paul Weitz          6.91
10 Steven Spielberg    6.79
# ℹ 791 more rows