Assignment 08

Author

Jamal Butt

Go to the shared posit.cloud workspace for this class and open the assign08 project. 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.

library(ggplot2)

data <- data.frame(
  Section = c("BUS 345", "MBA 674"),
  Students = c(30, 60)
)

ggplot(data, aes(x = Section, y = Students)) +
  geom_bar(stat = "identity", fill = "gray") +
  labs(title = "Total students by course, Spring 2015",
       x = "Section",
       y = "Number of students") +
  theme_minimal(base_size = 15)

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.

enrollment_data <- data.frame(
  Year = rep(2015, ),
  Semester = c("Fall", "Fall", "Spring", "Spring"),
  Course_Name = c("BUS 345", "MBA 674", "BUS 345", "MBA 674"),
  Section_ID = c(101, 102, 103, 104),
  Number_of_Students = c(30, 60, 25, 55)
)

sorted_data <- enrollment_data %>%
  mutate(Semester = factor(Semester, levels = c("Fall", "Spring"))) %>%
  arrange(Semester)

print(sorted_data)
  Year Semester Course_Name Section_ID Number_of_Students
1 2015     Fall     BUS 345        101                 30
2 2015     Fall     MBA 674        102                 60
3 2015   Spring     BUS 345        103                 25
4 2015   Spring     MBA 674        104                 55

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().

data <- data.frame(
  Section = c("BUS 345-25822", "BUS 377-68813", "MBA 674-29369", "MBA 674-42666", "MBA 676-38737", "MBA 676-86362"),
  Average_Grade = c(75, 78, 80, 79, 82, 81)
)
overall_avg <- mean(data$Average_Grade)

ggplot(data, aes(x = Average_Grade, y = reorder(Section, Average_Grade))) +
  geom_bar(stat = "identity", fill = "blue") +
  geom_vline(xintercept = overall_avg, color = "red", linetype = "solid", 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(base_size = 14)

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.

student_data <- data.frame(
  student_id = c(101, 102, 103, 104, 105, 106),
  last_name = c("Smith", "King", "Carlos", "snw", "Jones", "Garcia"),
  first_name = c("James", "Emily", "Zac", "Sarah", "John", "David"),
  course_name = c("MBA 674", "MBA 674", "MBA 674", "MBA 674", "BUS 345", "MBA 674"),
  semester = c("Spring", "Spring", "Spring", "Spring", "Spring", "Spring"),
  year = c(2015, 2015, 2015, 2015, 2015, 2015),
  final_avg = c(62, 80, 59, 90, 88, 64)
)

failed_students <- student_data %>%
  filter(course_name == "MBA 674" & semester == "Spring" & year == 2015 & final_avg < 65) %>%
  select(student_id, last_name, first_name)

print(failed_students)
  student_id last_name first_name
1        101     Smith      James
2        103    Carlos        Zac
3        106    Garcia      David

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.