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.
Recreate the graph below showing the total students by course in Spring 2015.
data <-data.frame(Section =c("BUS 345", "MBA 674"),Number_of_students =c(31, 64) )ggplot(data, aes(x = Section, y = Number_of_students)) +geom_bar(stat ="identity", fill ="grey30") +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.
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(DBI)library(RSQLite)library(dplyr)library(ggplot2)con <-dbConnect(RSQLite::SQLite(), "Grades.sqlite")students_data <-dbReadTable(con, "students")sections_data <-dbReadTable(con, "sections")grades_data <-dbReadTable(con, "grades")grades_2015 <- grades_data %>%inner_join(sections_data, by ="section_id") %>%filter(year =="2015")section_averages <- grades_2015 %>%group_by(section_id) %>%summarize(average_final_grade =mean(final_avg, na.rm =TRUE))overall_average <-mean(section_averages$average_final_grade, na.rm =TRUE)ggplot(section_averages, aes(x = section_id, y = average_final_grade)) +geom_bar(stat ="identity", fill ="grey30") +geom_hline(yintercept = overall_average, color ="red", linetype ="dashed", linewidth =1) +labs(title ="Average Final Grade by Section for 2015",x ="Section",y ="Average Final Grade" ) +theme_minimal()
dbDisconnect(con)
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(DBI)library(RSQLite)library(dplyr)con <-dbConnect(RSQLite::SQLite(), "Grades.sqlite")students_data <-dbReadTable(con, "students")sections_data <-dbReadTable(con, "sections")grades_data <-dbReadTable(con, "grades")section_data <- sections_data %>%mutate(year =as.character(year))failed_students <- grades_data %>%inner_join(sections_data, by ="section_id") %>%inner_join(students_data, by ="student_id") %>%filter( name =="MBA 674", semester =="Spring", year =="2015", final_avg <65 ) %>%select(student_id, last_name, first_name)print(failed_students)