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 Blackboard 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"
nscc_student_data <- read.csv("C:/Users/aless/Downloads/nscc_student_data.csv")
nscc_student_data <- nscc_student_data
# Preview first 6 lines of dataset
head(nscc_student_data, n = 6)
## Gender PulseRate CoinFlip1 CoinFlip2 Height ShoeLength Age Siblings RandomNum
## 1 Female 64 5 5 62 11.00 19 4 797
## 2 Female 75 4 6 62 11.00 21 3 749
## 3 Female 74 6 1 60 10.00 25 2 13
## 4 Female 65 4 4 62 10.75 19 1 613
## 5 Female NA NA NA 66 NA 26 6 53
## 6 Female 72 6 5 67 9.75 21 1 836
## HoursWorking Credits Birthday ProfsAge Coffee VoterReg
## 1 35 13 July 5 31 No Yes
## 2 25 12 December 27 30 Yes Yes
## 3 30 6 January 31 29 Yes No
## 4 18 9 6-13 31 Yes Yes
## 5 24 15 02-15 32 No Yes
## 6 15 9 april 14 32 No Yes
# Structure of dataset
str(nscc_student_data)
## 'data.frame': 40 obs. of 15 variables:
## $ Gender : chr "Female" "Female" "Female" "Female" ...
## $ PulseRate : int 64 75 74 65 NA 72 72 60 66 60 ...
## $ CoinFlip1 : int 5 4 6 4 NA 6 6 3 7 6 ...
## $ CoinFlip2 : int 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 : int 19 21 25 19 26 21 19 24 24 20 ...
## $ Siblings : int 4 3 2 1 6 1 2 2 3 1 ...
## $ RandomNum : int 797 749 13 613 53 836 423 16 12 543 ...
## $ HoursWorking: int 35 25 30 18 24 15 20 0 40 30 ...
## $ Credits : int 13 12 6 9 15 9 15 15 13 16 ...
## $ Birthday : chr "July 5" "December 27" "January 31" "6-13" ...
## $ ProfsAge : int 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
dim(nscc_student_data)
## [1] 40 15
The dimensions of the “nscc_student_data” data set is 40 x 15. That means it has 40 rows and 15 columns.
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
In the data set, there are two unknown values of student pulse rates.
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.
#This code allows us to calculate the mean of the pulse rate variable while ignoring unknown values.
mean(nscc_student_data$PulseRate, na.rm = TRUE)
## [1] 73.47368
#This code allows us to calculate the median of the pulse rate variable while ignoring unknown values.
median(nscc_student_data$PulseRate, na.rm = TRUE)
## [1] 70.5
#This code allows us to find the sample standard deviation of the pulse rates while ignoring unknown values.
sd(nscc_student_data$PulseRate, na.rm = TRUE)
## [1] 12.51105
The median and mean do not differ much. The mean value is 73.5 and the median value is 70.5. That’s a difference of about 3. However, the median would provide a more accurate center of this variable because outliers influence the value of the mean and can provide an inaccurate center of data.
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.
#This code allows us to calculate the Five Number Summary of the pulse rate variable.
summary(nscc_student_data$PulseRate)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 50.00 64.25 70.50 73.47 83.75 98.00 2
#This code allows us to calculate the IQR of the pulse rate variable.
IQR(nscc_student_data$PulseRate, na.rm = TRUE)
## [1] 19.5
#Below allows us to calculate the lower and upper fence to identify the outliers
19.5 * 1.5
## [1] 29.25
64.25 - 29.25
## [1] 35
83.75 + 29.25
## [1] 113
The five number summary of the PulseRate variable is:
Min = 50, Q1 = 64.25, Q2 = 70.50, Mean = 73.47, Q3 = 83.75, Max = 98
The IQR of the PulseRate variable is: 19.5
Any data values that are below 35 or above 113 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.
#This code creates a table to represent how many students identified as male or female.
table(nscc_student_data$Gender)
##
## Female Male
## 27 13
#This code creates a barplot to represent how many students identified as male or female.
barplot(table(nscc_student_data$Gender), main = "Gender of Students")
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 male student pulse rate
summary(NSCC_males$PulseRate)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 50.00 60.00 71.00 70.85 80.00 96.00
# Five Number Summary of female student pulse rate
summary(NSCC_females$PulseRate)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 56.00 65.00 70.00 74.84 88.00 98.00 2
The Five Number Summary of each data set are:
Males: Min = 50, Q1 = 60, Q2 = 71, Mean = 70.85, Q3 = 80, Max = 96
Females: Min = 56, Q1 = 65, Q2 = 70, Mean = 74.84, Q3 = 88, Max = 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)
The first difference I notice is the spread of data. Boxplot 1 has a larger spread, ranging from 50 to 96, while boxplot 2 ranges only from 56 to 98. Another difference is between the 75 percentile and 25 percentiles of the data. When it comes to boxplot 2, we see that a lot of the values within the data set are greater than the median. This indicates that it is right-skewed, along with our five number summary that shows the mean (74.84) is greater than the median (70).
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
# Females Coffee Drinkers
table(NSCC_females$Coffee)
##
## No Yes
## 7 20
# Percent of Males that Drink Coffee
(10/13)*100
## [1] 76.92308
# Percent of Females that Drink Coffee
(20/27)*100
## [1] 74.07407
When it comes to the male students of this data set, 10 drink coffee and 3 do not. This means 76.9% of these male students drink coffee. When it comes to the female students of this data set, 20 drink coffee and 7 do not. This means that 74.1% of female students drink coffee. There is no noticeable difference in coffee drinking based on gender because the difference is 2.8%.