We have a data set that is given to us that is a statisfaction survey that we added into the notebook with all of its data. It shows the respodent ID, Age, gender statisfaction and department.
We are intrested in the Satisfaction Score of our employees.
So we need to extract the Satisfaction Score.
First we are going to get the vector by running the code
# Extract the vector of satisfaction ratings
satisfaction_ratings <- Satisfactionsurvey$Satisfaction
# View the extracted vector
satisfaction_ratings
[1] 8 6 7 5 9 7 4 6 8 7 9 6 7 8 5 8 7 6 7 5
# Compute the mean and median satisfaction ratings
mean_satisfaction <- mean(satisfaction_ratings)
median_satisfaction <- median(satisfaction_ratings)
# Print the results
mean_satisfaction
[1] 6.75
median_satisfaction
[1] 7
Question 2: create a vector containing the ages of respondents who are in the “HR” department what are their average ages.
to do this we must sort by department in HR and then find the mean of ages. We will run this code to do so.
# Create a vector containing the ages of respondents in the HR department
hr_ages <- Satisfactionsurvey$Age[Satisfactionsurvey$Department == "HR"]
# View the HR ages vector
hr_ages
[1] 25 30 29 27 39
# Compute the average age of HR respondents
mean_hr_age <- mean(hr_ages)
# Print the result
mean_hr_age
[1] 30
To convert the Age and Satisfaction columns
into a numeric matrix, we subset those two columns from
Satisfactionsurvey and pass them to
as.matrix(). Then, we use tapply() directly on
the matrix columns to compute the mean satisfaction rating by age
group.
# Convert Age and Satisfaction columns into a matrix
age_sat_matrix <- as.matrix(Satisfactionsurvey[, c("Age", "Satisfaction")])
# View the first few rows and verify it is a matrix
head(age_sat_matrix)
Age Satisfaction
[1,] 25 8
[2,] 34 6
[3,] 28 7
[4,] 45 5
[5,] 30 9
[6,] 22 7
is.matrix(age_sat_matrix)
[1] TRUE
# Option A: Mean satisfaction for each individual age in the matrix
mean_by_exact_age <- tapply(age_sat_matrix[, "Satisfaction"],
age_sat_matrix[, "Age"],
mean)
mean_by_exact_age
22 25 26 27 28 29 30 31 33
7.0 8.0 5.0 7.0 6.5 8.0 9.0 7.0 8.0
34 35 38 39 40 42 45 46 50
6.0 6.0 6.0 7.0 4.0 8.0 5.0 7.0 7.0
# Option B: Mean satisfaction by age group brackets (20-29, 30-39, 40+)
age_groups <- cut(age_sat_matrix[, "Age"],
breaks = c(20, 29, 39, 50),
labels = c("20-29", "30-39", "40-50"))
mean_by_age_group <- tapply(age_sat_matrix[, "Satisfaction"],
age_groups,
mean)
mean_by_age_group
20-29 30-39 40-50
6.857143 7.000000 6.200000
Question 4 Mean of each department in a matrix and how we transpose columns and calulate the mean of each column WE do this by making a matrix and then grouping by separtment and then using ‘t()’ to flip it to change columns and rows
# Compute mean satisfaction rating by department
dept_means <- tapply(Satisfactionsurvey$Satisfaction,
Satisfactionsurvey$Department,
mean)
# Create a matrix where each row is a department and the column is mean satisfaction
dept_matrix <- matrix(dept_means,
ncol = 1,
dimnames = list(names(dept_means), "Mean_Satisfaction"))
# Display the original 4x1 matrix
dept_matrix
Mean_Satisfaction
Finance 6.4
HR 7.8
IT 6.4
Marketing 6.4
# Transpose the matrix using t()
transposed_matrix <- t(dept_matrix)
# Display the transposed 1x4 matrix
transposed_matrix
Finance HR IT Marketing
Mean_Satisfaction 6.4 7.8 6.4 6.4
# Calculate the mean of each column in the transposed matrix
col_means <- colMeans(transposed_matrix)
col_means
Finance HR IT Marketing
6.4 7.8 6.4 6.4
Question 5: turn this into a data frame. How many rows and collums and provide a summary of the gender and department columns.
Add a datat frame and add code to check how many n columns and n rows we have and then a summary of the two columns gender and department.
# Verify Satisfactionsurvey is a data frame
is.data.frame(Satisfactionsurvey)
[1] TRUE
# Check the number of rows and columns
dim(Satisfactionsurvey)
[1] 20 5
nrow(Satisfactionsurvey)
[1] 20
ncol(Satisfactionsurvey)
[1] 5
# Ensure Gender and Department are treated as factors (in case Excel imported them as plain text)
Satisfactionsurvey$Gender <- as.factor(trimws(Satisfactionsurvey$Gender))
Satisfactionsurvey$Department <- as.factor(trimws(Satisfactionsurvey$Department))
# Provide a summary of the Gender and Department columns
summary(Satisfactionsurvey[, c("Gender", "Department")])
Gender Department
Female :8 Finance :5
Male :8 HR :5
Non-binary:4 IT :5
Marketing:5
Question 6: What is the average satisfactory rating between for respondents between 30 and 40 years of age
We srink the data frame to those 30-40 years old and we find the mean statisfactory rating.
# Filter the data frame to include only respondents aged between 30 and 40
age_30_40 <- subset(Satisfactionsurvey, Age >= 30 & Age <= 40)
# View the filtered data frame
age_30_40
# Compute the average satisfaction rating for this age group
mean_sat_30_40 <- mean(age_30_40$Satisfaction)
# Print the result
mean_sat_30_40
[1] 6.666667
Question number 7: create a historgrahm of the Age column what does this tell us about the ages.
First we run code to make a histogram as we learned in project 2 and then we summurixe after seeing the histogram.
# Create a histogram of the Age column
hist(Satisfactionsurvey$Age,
main = "Distribution of Respondent Ages",
xlab = "Age (Years)",
ylab = "Frequency",
col = "steelblue",
border = "white")
# View summary statistics to support the visual interpretation
summary(Satisfactionsurvey$Age)
Min. 1st Qu. Median Mean 3rd Qu. Max.
22.00 28.00 32.00 33.95 39.25 50.00
This shows that our employes are mainly 25-40 but 2 are younger than 25 and 4 are 40-50 When you save the notebook, an HTML file containing the code and output will be saved
question 8: plot a histogram and conclude
We use what we learned in project 2 to plot a hstogram and then we will review and see what we notice.
# Plot a histogram of the Satisfaction ratings across the full 1-10 scale
hist(Satisfactionsurvey$Satisfaction,
breaks = seq(0.5, 10.5, by = 1),
xlim = c(1, 10),
xaxt = "n",
main = "Distribution of Satisfaction Ratings",
xlab = "Satisfaction Rating (1 to 10)",
ylab = "Frequency",
col = "mediumseagreen",
border = "white")
# Add integer tick marks from 1 to 10 on the x-axis
axis(1, at = 1:10)
# Display exact frequency counts for each rating
table(Satisfactionsurvey$Satisfaction)
4 5 6 7 8 9
1 3 4 6 4 2
we see a normal distrubution with a mean at 7. This shows that most people are 7 out of 10 satisfied no one is lower than 3 or at 10. There are no gaps a peak is at 7.
Question 9: create a boxplot of satisfactory ratings by department. What can we infer about satisfcatory accross departments.
We create a boxplot like we learned in project 2 and we will evulate at the end.
# Create a boxplot of Satisfaction ratings by Department
boxplot(Satisfaction ~ Department,
data = Satisfactionsurvey,
main = "Satisfaction Ratings by Department",
xlab = "Department",
ylab = "Satisfaction Rating (1 to 10)",
col = c("lightblue", "lightgreen", "lightcoral", "lightyellow"),
ylim = c(1, 10))
# View the five-number summary for each department
tapply(Satisfactionsurvey$Satisfaction, Satisfactionsurvey$Department, summary)
$Finance
Min. 1st Qu. Median Mean 3rd Qu. Max.
4.0 6.0 7.0 6.4 7.0 8.0
$HR
Min. 1st Qu. Median Mean 3rd Qu. Max.
7.0 7.0 8.0 7.8 8.0 9.0
$IT
Min. 1st Qu. Median Mean 3rd Qu. Max.
5.0 6.0 6.0 6.4 7.0 8.0
$Marketing
Min. 1st Qu. Median Mean 3rd Qu. Max.
5.0 5.0 6.0 6.4 7.0 9.0
The first thing we notice is the outlier in finacne meaning one employe there is very unsatisfied compared to his peers in his departmen. HR tends to be the most satisfied while Marketing has the most varience within the department.
Question 10: create a age by gender boxplot are there differnt distrubutions between genders.
We will make anouther box plot and summerise our findings.
# Generate a boxplot for Age by Gender
boxplot(Age ~ Gender,
data = Satisfactionsurvey,
main = "Respondent Age Distribution by Gender",
xlab = "Gender",
ylab = "Age (Years)",
col = c("plum", "skyblue", "palegreen"))
# View the five-number summary for each gender
tapply(Satisfactionsurvey$Age, Satisfactionsurvey$Gender, summary)
$Female
Min. 1st Qu. Median Mean 3rd Qu. Max.
22.00 26.50 28.50 28.12 30.25 33.00
$Male
Min. 1st Qu. Median Mean 3rd Qu. Max.
26.00 33.25 38.50 37.88 41.25 50.00
$`Non-binary`
Min. 1st Qu. Median Mean 3rd Qu. Max.
28.00 33.25 38.50 37.75 43.00 46.00
There is a big differnce as females tend to be less satisfied than males and non-binary people.
Question 11: Use IQR to find outliers.
We will use IQR to find outliers meaning we find anything that is more than 2 Standard devations away from the mean.
# Compute Q1, Q3, and IQR for Satisfaction ratings
Q1 <- quantile(Satisfactionsurvey$Satisfaction, 0.25)
Q3 <- quantile(Satisfactionsurvey$Satisfaction, 0.75)
IQR_val <- IQR(Satisfactionsurvey$Satisfaction)
# Compute the lower and upper outlier bounds (1.5 * IQR rule)
lower_bound <- Q1 - 1.5 * IQR_val
upper_bound <- Q3 + 1.5 * IQR_val
# Display the quartiles, IQR, and fences
Q1
25%
6
Q3
75%
8
IQR_val
[1] 2
lower_bound
25%
3
upper_bound
75%
11
# Identify and list any outliers in the Satisfaction ratings
sat_outliers <- Satisfactionsurvey$Satisfaction[
Satisfactionsurvey$Satisfaction < lower_bound |
Satisfactionsurvey$Satisfaction > upper_bound
]
sat_outliers
numeric(0)
# Double-check using R's built-in boxplot.stats()
boxplot.stats(Satisfactionsurvey$Satisfaction)$out
numeric(0)
There are no outliers as everything falls between 4 and 9
question 12:Using the boxplot of Satisfaction ratings by Department, identify if there are any departments with outliers in satisfaction ratings. How do these outliers impact the overall satisfaction analysis?
We are going re run the boxplot from question 9 and anylize.
# Generate the boxplot and save its underlying statistics
bp_dept <- boxplot(Satisfaction ~ Department,
data = Satisfactionsurvey,
main = "Satisfaction Ratings by Department",
xlab = "Department",
ylab = "Satisfaction Rating (1 to 10)",
col = c("lightblue", "lightgreen", "lightcoral", "lightyellow"),
ylim = c(1, 10))
# Identify any outlier values and the department they belong to
bp_dept$out
[1] 4
bp_dept$names[bp_dept$group]
[1] "Finance"
# View the specific outlier record in Satisfactionsurvey
subset(Satisfactionsurvey, Department == "Finance" & Satisfaction == 4)
# Compare Finance mean WITH vs. WITHOUT the outlier
mean(Satisfactionsurvey$Satisfaction[Satisfactionsurvey$Department == "Finance"])
[1] 6.4
mean(Satisfactionsurvey$Satisfaction[Satisfactionsurvey$Department == "Finance" &
Satisfactionsurvey$Satisfaction != 4])
[1] 7
# Compare overall company mean WITH vs. WITHOUT the outlier
mean(Satisfactionsurvey$Satisfaction)
[1] 6.75
mean(Satisfactionsurvey$Satisfaction[Satisfactionsurvey$Satisfaction != 4])
[1] 6.894737
Finance has one outlier and he is bringing their mean down as he is more than 1.5 IQR from the median this means they would be the 2nd most satisfied if it wasnt for him.
alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.