Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Running Code
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
# ============================================================# WEEK 2A: SQL AND R - MOVIE RATINGS# ============================================================# ------------------------------------------------------------# 1. LOAD REQUIRED PACKAGES# ------------------------------------------------------------library(DBI)library(RSQLite)library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
# ------------------------------------------------------------# 4. REMOVE OLD TABLES# This makes the script reproducible when rerun.# ------------------------------------------------------------dbExecute(con, "DROP TABLE IF EXISTS ratings;")
# ------------------------------------------------------------# 6. CREATE MOVIES TABLE# ------------------------------------------------------------result_movies <-dbExecute(con, "CREATE TABLE movies ( movie_id INTEGER PRIMARY KEY, title TEXT NOT NULL);")print("Movies table created.")
# ------------------------------------------------------------# 10. INSERT SIX MOVIES# ------------------------------------------------------------insert_movies <-dbExecute(con, "INSERT INTO movies(movie_id, title)VALUES(1, 'Superman'),(2, 'Weapons'),(3, 'Jurassic World: Rebirth'),(4, 'Thunderbolts*'),(5, 'Mission: Impossible - The Final Reckoning'),(6, 'F1: The Movie');")print("Number of movies inserted:")
movie_id title
1 1 Superman
2 2 Weapons
3 3 Jurassic World: Rebirth
4 4 Thunderbolts*
5 5 Mission: Impossible - The Final Reckoning
6 6 F1: The Movie
# ------------------------------------------------------------# 11. INSERT RATINGS# NULL = participant did not rate the movie# ------------------------------------------------------------insert_ratings <-dbExecute(con, "INSERT INTO ratings(critic_id, movie_id, rating)VALUES(1,1,5),(1,2,4),(1,3,3),(1,4,4),(1,5,5),(1,6,5),(2,1,4),(2,2,NULL),(2,3,3),(2,4,4),(2,5,4),(2,6,5),(3,1,5),(3,2,5),(3,3,NULL),(3,4,4),(3,5,5),(3,6,4),(4,1,4),(4,2,4),(4,3,2),(4,4,NULL),(4,5,4),(4,6,5),(5,1,5),(5,2,3),(5,3,4),(5,4,5),(5,5,NULL),(5,6,4);")print("Number of ratings inserted:")
# ------------------------------------------------------------# 12. PRINT ALL THREE TABLES# ------------------------------------------------------------print("====================================")
# ------------------------------------------------------------# 13. CREATE SQL JOIN QUERY# ------------------------------------------------------------movie_query <-"SELECT r.critic_id, c.critic_name, r.movie_id, m.title, r.ratingFROM ratings AS rJOIN critics AS c ON r.critic_id = c.critic_idJOIN movies AS m ON r.movie_id = m.movie_idORDER BY r.critic_id, r.movie_id;"print("SQL JOIN QUERY:")
[1] "SQL JOIN QUERY:"
print(movie_query)
[1] "\nSELECT\n r.critic_id,\n c.critic_name,\n r.movie_id,\n m.title,\n r.rating\nFROM ratings AS r\n\nJOIN critics AS c\n ON r.critic_id = c.critic_id\n\nJOIN movies AS m\n ON r.movie_id = m.movie_id\n\nORDER BY\n r.critic_id,\n r.movie_id;\n"
# ------------------------------------------------------------# 14. LOAD SQL DATA INTO R AS A DATAFRAME# ------------------------------------------------------------ratings_df <-dbGetQuery( con, movie_query)print("JOINED R DATAFRAME")
# A tibble: 5 × 7
critic_name Superman Weapons `Jurassic World: Rebirth` `Thunderbolts*`
<chr> <int> <int> <int> <int>
1 Critic 1 5 4 3 4
2 Critic 2 4 NA 3 4
3 Critic 3 5 5 NA 4
4 Critic 4 4 4 2 NA
5 Critic 5 5 3 4 5
# ℹ 2 more variables: `Mission: Impossible - The Final Reckoning` <int>,
# `F1: The Movie` <int>
print("Rating matrix dimensions:")
[1] "Rating matrix dimensions:"
print(dim(rating_matrix))
[1] 5 7
# ------------------------------------------------------------# 20. SQL SUMMARY QUERY# Average rating directly from SQL# ------------------------------------------------------------sql_movie_summary <-dbGetQuery(con, "SELECT m.movie_id, m.title, COUNT(r.rating) AS number_of_ratings, ROUND(AVG(r.rating), 2) AS average_ratingFROM movies AS mLEFT JOIN ratings AS r ON m.movie_id = r.movie_idGROUP BY m.movie_id, m.titleORDER BY average_rating DESC;")print("MOVIE SUMMARY CALCULATED IN SQL")
[1] "MOVIE SUMMARY CALCULATED IN SQL"
print(sql_movie_summary)
movie_id title number_of_ratings
1 1 Superman 5
2 6 F1: The Movie 5
3 5 Mission: Impossible - The Final Reckoning 4
4 4 Thunderbolts* 4
5 2 Weapons 4
6 3 Jurassic World: Rebirth 4
average_rating
1 4.60
2 4.60
3 4.50
4 4.25
5 4.00
6 3.00
# ------------------------------------------------------------# 21. SQL QUERY FOR MISSING RATINGS# ------------------------------------------------------------sql_missing <-dbGetQuery(con, "SELECT c.critic_name, m.title, r.ratingFROM ratings AS rJOIN critics AS c ON r.critic_id = c.critic_idJOIN movies AS m ON r.movie_id = m.movie_idWHERE r.rating IS NULL;")print("MISSING RATINGS FOUND USING SQL")
[1] "MISSING RATINGS FOUND USING SQL"
print(sql_missing)
critic_name title rating
1 Critic 2 Weapons NA
2 Critic 3 Jurassic World: Rebirth NA
3 Critic 4 Thunderbolts* NA
4 Critic 5 Mission: Impossible - The Final Reckoning NA
# ------------------------------------------------------------# 22. GRAPH 1 - AVERAGE MOVIE RATINGS# ------------------------------------------------------------graph1 <-ggplot( movie_summary,aes(x =reorder( title, average_rating ),y = average_rating )) +geom_col() +coord_flip() +labs(title ="Average Movie Ratings",subtitle ="Ratings on a 1-5 Scale",x ="Movie",y ="Average Rating" ) +ylim(0, 5) +theme_minimal()print(graph1)
# ------------------------------------------------------------# 23. GRAPH 2 - NUMBER OF RATINGS PER MOVIE# ------------------------------------------------------------graph2 <-ggplot( movie_summary,aes(x =reorder( title, number_of_ratings ),y = number_of_ratings )) +geom_col() +coord_flip() +labs(title ="Number of Ratings per Movie",x ="Movie",y ="Number of Ratings" ) +theme_minimal()print(graph2)