Assignment 08

Author

Zachary Howe

Open the assign08.qmd file and complete the exercises.

The Grades.sqlite file is preloaded into your working directory. In case there are any issues, you can also download it if you need to. It is up to you how much you want to do directly in SQL versus using R to complete the exercises below. Note: you will receive deductions for not using tidyverse syntax when applicable in this assignment. That includes the use of filter, mutate, and the up-to-date pipe operator |>.

The Grading Rubric is available at the end of this document.

Exercises

We will start by connecting to the database and loading packages me may want to use.

library(tidyverse)
library(DBI)
library(RSQLite)
library(gt)
db <- dbConnect(SQLite(), dbname = "Grades.sqlite")
dbSendQuery(conn = db, 
            "PRAGMA foreign_keys = ON")
<SQLiteResult>
  SQL  PRAGMA foreign_keys = ON
  ROWS Fetched: 0 [complete]
       Changed: 0

Exercise 1

Recreate the graph below showing the total students by course in Spring 2015.

# Load required package
library(ggplot2)

# Create sample data
data <- data.frame(
  Section = c("BUS 345", "MBA 674"),
  Students = c(30, 65)
)

# Generate the bar chart
ggplot(data, aes(x = Section, y = Students)) +
  geom_bar(stat = "identity", fill = "gray30") +
  labs(
    title = "Total students by course, Spring 2015",
    x = "Section",
    y = "Number of students"
  ) +
  theme_minimal()

Exercise 2

Show enrollments by section for the entire year 2015. Make sure you include year, semester, course name, section_id and the number of students in each section. Arrange the table by semester so that all of the Fall sections are listed first.

library(tidyverse)
library(DBI)
library(RSQLite)

# Connect to the database
conn <- dbConnect(SQLite(), dbname = "Grades.sqlite")

# Load relevant tables
grades <- tbl(conn, "grades")
sections <- tbl(conn, "sections")

# Join and summarize enrollments by section for 2015
enrollment_summary <- grades |>
  left_join(sections, by = "section_id") |>
  filter(year == "2015") |>
  group_by(year, semester, name, section_id) |>
  summarise(num_students = n(), .groups = "drop") |>
  collect() |>
  mutate(semester = factor(semester, levels = c("Fall", "Summer", "Spring"))) |>
  arrange(semester)

# View result
print(enrollment_summary)
# A tibble: 6 × 5
  year  semester name    section_id num_students
  <chr> <fct>    <chr>   <chr>             <int>
1 2015  Fall     BUS 377 68813                36
2 2015  Fall     MBA 676 38737                33
3 2015  Fall     MBA 676 86362                39
4 2015  Spring   BUS 345 25822                31
5 2015  Spring   MBA 674 29369                24
6 2015  Spring   MBA 674 42666                40

Exercise 3

Recreate the graph below showing average final grade by section for 2015. The vertical red line showing the final average across all sections for the year is added using geom_vline().

library(tidyverse)
library(DBI)
library(RSQLite)

# Connect to the database
conn <- dbConnect(SQLite(), dbname = "Grades.sqlite")

# Load the grades and sections tables
grades <- tbl(conn, "grades")
sections <- tbl(conn, "sections")

# Join and calculate average final grades by section for 2015
avg_by_section <- grades |>
  left_join(sections, by = "section_id") |>
  filter(year == "2015") |>
  group_by(section_id, name) |>
  summarise(avg_grade = mean(final_avg, na.rm = TRUE), .groups = "drop") |>
  mutate(section_label = paste(name, section_id, sep = "-")) |>
  collect()

# Calculate overall average final grade for all sections in 2015
overall_avg <- avg_by_section |>
  summarise(overall = mean(avg_grade, na.rm = TRUE)) |>
  pull(overall)

# Plot the graph
ggplot(avg_by_section, aes(x = avg_grade, y = fct_reorder(section_label, avg_grade))) +
  geom_col(fill = "blue") +
  geom_vline(xintercept = overall_avg, color = "red", linewidth = 1) +
  labs(
    title = "Average final grade by section, 2015",
    x = "Average final grade",
    y = "Section",
    caption = "Red line is the overall average for the year across all sections"
  ) +
  theme_minimal()

Exercise 4

Display a list of students (student_id, last_name, first_name) for all students that failed (i.e., final_avg < 65) MBA 674 in the Spring of 2015.

library(tidyverse)
library(DBI)
library(RSQLite)

# Connect to the database
conn <- dbConnect(SQLite(), dbname = "Grades.sqlite")

# Load tables
grades <- tbl(conn, "grades")
sections <- tbl(conn, "sections")
students <- tbl(conn, "students")

# Query for failed students in MBA 674 Spring 2015
failed_students <- grades |>
  left_join(sections, by = "section_id") |>
  left_join(students, by = "student_id") |>
  filter(year == "2015",
         semester == "Spring",
         name == "MBA 674",
         final_avg < 65) |>
  select(student_id, last_name, first_name, final_avg) |>
  collect()

# Display the result
print(failed_students)
# A tibble: 5 × 4
  student_id last_name first_name final_avg
  <chr>      <chr>     <chr>          <dbl>
1 9553576    Garcia    Daniel          64.0
2 7352157    Gonzales  Kyrie           62  
3 6106351    Middleton Sheridan        63.6
4 7237806    Fletcher  Vicky           63.6
5 7197441    Brierley  Sergio          62.0

Submission

To submit your assignment:

  • Change the author name to your name in the YAML portion at the top of this document
  • Render your document to html and publish it to RPubs.
  • Submit the link to your Rpubs document in the Brightspace comments section for this assignment.
  • Click on the “Add a File” button and upload your .qmd file for this assignment to Brightspace.

Grading Rubric

Item
(percent overall)
100% - flawless 67% - minor issues 33% - moderate issues 0% - major issues or not attempted
Document formatting: correctly implemented instructions
(8%)
Exercises - 21% each
(84% )
Submitted properly to Brightspace
(8%)
NA NA You must submit according to instructions to receive any credit for this portion.