This project will demonstrate your ability to do exploratory data analysis on single variables of data in R and RStudio. The entire project will use the NSCC Student Dataset, which you will need to load into R in question 1.
Download the “nscc_student_data.csv” file from MyOpenMath and use the read.csv() function to store it into an object called “nscc_student_data”. Print the first few lines of the dataset using the head() function. Also print the structure of the dataset using the str() function.
# Load dataset in and store as object "nscc_student_data"
# This code loads the dataset and stores it as an object
library(readxl)
nscc_student_data <- read_excel("~/Desktop/misc/nscc_student_data.xls")
View(nscc_student_data)
nscc_student_data <- nscc_student_data
# Preview first 6 lines of dataset
# This code allows us to view the first 6 lines of the data set
head(nscc_student_data)
## # A tibble: 6 x 15
## Gender PulseRate CoinFlip1 CoinFlip2 Height ShoeLength Age Siblings
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Female 64 5 5 62 11 19 4
## 2 Female 75 4 6 62 11 21 3
## 3 Female 74 6 1 60 10 25 2
## 4 Female 65 4 4 62 10.8 19 1
## 5 Female NA NA NA 66 NA 26 6
## 6 Female 72 6 5 67 9.75 21 1
## # … with 7 more variables: RandomNum <dbl>, HoursWorking <dbl>,
## # Credits <dbl>, Birthday <chr>, ProfsAge <dbl>, Coffee <chr>,
## # VoterReg <chr>
# Structure of dataset
# This code allows us to view the structure of the dataset
str(nscc_student_data)
## Classes 'tbl_df', 'tbl' and 'data.frame': 40 obs. of 15 variables:
## $ Gender : chr "Female" "Female" "Female" "Female" ...
## $ PulseRate : num 64 75 74 65 NA 72 72 60 66 60 ...
## $ CoinFlip1 : num 5 4 6 4 NA 6 6 3 7 6 ...
## $ CoinFlip2 : num 5 6 1 4 NA 5 6 5 8 5 ...
## $ Height : num 62 62 60 62 66 ...
## $ ShoeLength : num 11 11 10 10.8 NA ...
## $ Age : num 19 21 25 19 26 21 19 24 24 20 ...
## $ Siblings : num 4 3 2 1 6 1 2 2 3 1 ...
## $ RandomNum : num 797 749 13 613 53 836 423 16 12 543 ...
## $ HoursWorking: num 35 25 30 18 24 15 20 0 40 30 ...
## $ Credits : num 13 12 6 9 15 9 15 15 13 16 ...
## $ Birthday : chr "43651" "43826" "43496" "43629" ...
## $ ProfsAge : num 31 30 29 31 32 32 28 28 31 28 ...
## $ Coffee : chr "No" "Yes" "Yes" "Yes" ...
## $ VoterReg : chr "Yes" "Yes" "No" "Yes" ...
a.) What are the dimensions of the nscc_student_data dataframe?
# Find the dimensions of the nscc_student_data dataframe
# This code finds the dimensions of the nscc_student_data dataset
dim(nscc_student_data)
## [1] 40 15
b.) The chunk of code below will tell you how many values in the PulseRate variable exist (FALSE) and how many are NA (TRUE). How many values are in the variable are missing?
# How many values in PulseRate variable are missing
table(is.na(nscc_student_data$PulseRate))
##
## FALSE TRUE
## 38 2
There are 2 missing values in the PulseRate variable
Use an r chunk to calculate the mean, median, and sample standard deviation of the pulse rate variable. Do the mean and median differ by much? If yes, explain why and which would be a better choice as the “center” or “average” of this variable.
mean(nscc_student_data$PulseRate, na.rm = TRUE)
## [1] 73.47368
median(nscc_student_data$PulseRate, na.rm = TRUE)
## [1] 70.5
sd(nscc_student_data$PulseRate, na.rm = TRUE)
## [1] 12.51105
The mean of the PulseRate variable is 73.5, the median is 70.5, and the standard deviation is 12.5. The mean and median do not differ by much.
Use an r chunk to calculate the Five Number Summary and IQR of the pulse rate variable? Clearly state your answer for each below the r chunk. Based on the definition of outliers being more than 1.5 IQRs below Q1 or above Q3, are there any values in the pulse rate variable that are considered outliers? Clearly state below the thresholds for a data to be considered an outlier.
fivenum(nscc_student_data$PulseRate, na.rm = TRUE)
## [1] 50.0 64.0 70.5 85.0 98.0
IQR(nscc_student_data$PulseRate, na.rm = TRUE)
## [1] 19.5
The five number summary of the PulseRate variable is: 50.0, 64.0,70.5,85.0,98.0 The IQR of the PulseRate variable is: 19.5
Any data values that are below (34.75) or above (114.25) would be considered to be outliers. Based on that, there (are not) any outliers.
The Gender variable gives whether students identified as male or female. Create a table and a barplot of that variable.
table(nscc_student_data$Gender)
##
## Female Male
## 27 13
barplot(table(nscc_student_data$Gender))
Split the dataframe into two subsets – one that has all the males and another that has all the females. Store them into objects called “NSCC_males” and “NSCC_females”. The first one has been done for you as a template.
# Create males subset
NSCC_males <- subset(nscc_student_data, nscc_student_data$Gender == "Male")
# Create females subset
NSCC_females <- subset(nscc_student_data, nscc_student_data$Gender == "Female")
Use an r chunk below to generate the information which will give you the Five Number Summary for the pulse rate variable for each of the male and female subsets.
# Five Number Summary of males subset
fivenum(NSCC_males$PulseRate)
## [1] 50 60 71 80 96
# Five Number Summary of females subset
fivenum(NSCC_females$PulseRate)
## [1] 56 65 70 88 98
The Five Number Summary of each dataset are:
Males: 50,60,71,80,96 Females: 56,65,70,88,98
Create side-by-side boxplots for the pulse rate variable each of the male and female subsets. Is there any noticeable difference between the two subsets?
# Create side-by-side boxplots for each subset
boxplot(NSCC_males$PulseRate, NSCC_females$PulseRate)
Yes, there is a noticeable difference between the two subsets.
Create a frequency distribution for how many males and females answered “Yes” or “No” to the variable “Coffee” by using the table() function. What percent of this sample of NSCC students drink coffee? Is there any noticeable difference in coffee drinking based on gender?
# Male Coffee Drinkers
table(NSCC_males$Coffee)
##
## No Yes
## 3 10
#There are 10 males that drink coffee, and 3 males that do not drink coffee.
# Females Coffee Drinkers
table(NSCC_females$Coffee)
##
## No Yes
## 7 20
#There are 20 females that drink coffee, and 7 females that do not drink coffee.
# Percent of Males that Drink Coffee
round(prop.table(table(NSCC_males$Coffee))*100, digits=1)
##
## No Yes
## 23.1 76.9
#The percent of males that drink coffee is 76.9%.
# Percent of Females that Drink Coffee
round(prop.table(table(NSCC_females$Coffee))*100, digits=1)
##
## No Yes
## 25.9 74.1
#The percent of females that drink coffee is 74.1%.
# There is no noticable difference in coffee drinking based on gender.