library(readxl)
Survey_data <- read_excel("~/Desktop/Math in A.I/Math In Tech/Survey_data.xlsx")

Vectors

We can use the $ operator to get a column from our data. I am saving the satisfaction ratings as Sat.

Sat <- Survey_data$Satisfaction
Sat
##  [1] 8 6 7 5 9 7 4 6 8 7 9 6 7 8 5 8 7 6 7 5
mean(Sat)
## [1] 6.75
median(Sat)
## [1] 7

The mean satisfaction rating is 6.75, and our median is 7. So the ratings are centered around 7 out of 10.

HR Ages

I am selecting the ages from HR and finding their average.

HR_ages <- Survey_data$Age[Survey_data$Department == "HR"]
HR_ages
## [1] 25 30 29 27 39
mean(HR_ages)
## [1] 30

There are 5 respondents in HR, and their average age is 30.

Matrices

I am using Age and Satisfactoin to make a matrix so I can compare satisfaction ratings by age group.

AgeSat <- as.matrix(Survey_data[, c("Age", "Satisfaction")])
AgeSat
##       Age Satisfaction
##  [1,]  25            8
##  [2,]  34            6
##  [3,]  28            7
##  [4,]  45            5
##  [5,]  30            9
##  [6,]  22            7
##  [7,]  40            4
##  [8,]  35            6
##  [9,]  29            8
## [10,]  50            7
## [11,]  31            9
## [12,]  38            6
## [13,]  27            7
## [14,]  42            8
## [15,]  26            5
## [16,]  33            8
## [17,]  39            7
## [18,]  28            6
## [19,]  46            7
## [20,]  31            5
age_decade <- paste0(floor(AgeSat[, "Age"] / 10) * 10, "s")

tapply(AgeSat[, "Satisfaction"], age_decade, mean)
##      20s      30s      40s      50s 
## 6.857143 7.000000 6.000000 7.000000

The 40s age group has the lowest mean satisfaction at 6. The 30s and 50s both average 7, but only 1 repsondent is in their 50s.

Department Matrix

I am finding the average satisfaction for each department, then creating and transposing a matrix.

dept_means <- aggregate(Satisfaction ~ Department,
                        data = Survey_data,
                        FUN = mean)

dept_matrix <- as.matrix(dept_means["Satisfaction"])
rownames(dept_matrix) <- dept_means$Department

dept_matrix
##           Satisfaction
## Finance            6.4
## HR                 7.8
## IT                 6.4
## Marketing          6.4
dept_transposed <- t(dept_matrix)
dept_transposed
##              Finance  HR  IT Marketing
## Satisfaction     6.4 7.8 6.4       6.4
colMeans(dept_transposed)
##   Finance        HR        IT Marketing 
##       6.4       7.8       6.4       6.4

HR has the highest average satisfaction at 7.8. Finance, IT, and Marketing each average 6.4

Data Frames

I am checking the size of our data frame andh how many respondents are in each gender and department category.

dim(Survey_data)  # shows rows, then columns
## [1] 20  5
nrow(Survey_data) # rows only
## [1] 20
ncol(Survey_data) # columns only
## [1] 5
table(Survey_data$Gender)
## 
##     Female       Male Non-binary 
##          8          8          4
table(Survey_data$Department)
## 
##   Finance        HR        IT Marketing 
##         5         5         5         5

Our data frame has 20 rows and 5 columns. There are 8 males, 8 females, and 4 non binary respondents. Each department has 5 people.

Filtering by Age

I am selecting respondents ages 30 through 40 and finding their average satisfaction rating.

Age30to40 <- Survey_data[Survey_data$Age >= 30 & Survey_data$Age <= 40, ]

mean(Age30to40$Satisfaction)
## [1] 6.666667
nrow(Age30to40)
## [1] 9

The are 9 respondents between 30 and 40 years old. Their average satisfaction rating is about 6.67.

Histograms

hist(Survey_data$Age,
     main = "Distribution of Respondent Ages",
     xlab = "Age",
     ylab = "Number of Respondents")

Most respondents are in their 20s and 30s, with fewer in the older age ranges.

Satisfaction Histogram

I am checking which satisfaction rating appear most often and whether therre are any gaps.

hist(Survey_data$Satisfaction,
     breaks = seq(3.5, 9.5, by = 1),
     main = "Satisfaction Ratings",
     xlab = "Satisfaction Score",
     ylab = "Number of Respondents")

The most common Satisfaction rating is 7. appearing 6 times. There are no gaps between the ratings of 4 and 9.

Boxplots

I am using a boxplot to compare satisfaction ratings across the departments.

boxplot(Satisfaction ~ Department,
        data = Survey_data,
        main = "Satisfaction Scores by Department",
        xlab = "Department",
        ylab = "Satisfaction Score")

HR has the highest median satisfaction rating at 8. Finance has a median of 7, and IT & Markerting have medians of 6. Finance has a low outlier of 4.

Age by Gender

I am comparing the ages for each gender category to see how their distributions differ.

boxplot(Age ~ Gender,
        data = Survey_data,
        main = "Ages by Gender",
        xlab = "Gender",
        ylab = "Age")

The female respondents have a lower median age of 28.5. The male and non-binary groups both have a median age of 38.5, and males have the widest age range.

Outliers

I am using the IQR method to see if any satisfaction ratings are unusually high or low.

q1 <- quantile(Sat, 0.25)
q3 <- quantile(Sat, 0.75)
iqr_value <- q3 - q1
q1
## 25% 
##   6
q3
## 75% 
##   8
iqr_value
## 75% 
##   2
lower <- q1 - 1.5 * iqr_value
upper <- q3 + 1.5 * iqr_value

outliers <- Sat[Sat < lower | Sat > upper]

lower
## 25% 
##   3
upper
## 75% 
##  11
outliers
## numeric(0)

Q1 is 6. Q3 is 8, and the IQR is 2. The LB is 3 and the UB is 11. There are no overall satisfaction outliers.

Department Outliers

I am checking for outliers within each dept and seeing how they affect the avg satisfaction.

bp <- boxplot(Satisfaction ~ Department,
              data = Survey_data,
              main = "Satisfaction Scores by Department",
              xlab = "Department",
              ylab = "Satisfaction Score")

data.frame(
  Department = bp$names[bp$group],
  Outlier = bp$out
)
Finance_scores <- Survey_data$Satisfaction[Survey_data$Department == "Finance"]

mean(Finance_scores)
## [1] 6.4
mean(Finance_scores[Finance_scores != 4])
## [1] 7

Finance has an outlier of 4. Witht that rating included, the mean is 6.4. Without it, the mean increases to 7. This shows how one low rating can affect the avg. The other depts did not have any outliers.