Vectors

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

The mean satisfaction rating is 6.75, and our median is 7. Satisfaction is around 7 out of 10.

HR Ages

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

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

Matrices

I will use the Age and Satisfaction columns to make a matrix and compare the average satisfaction by age group.

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

Department Matrix

I will make a matrix showing the average satisfaction for each department and then transpose it.

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

Data Frames

I will check how many rows and columnds our data has and summarize the Gender and Department columns.

The 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 will filter the data to include ages 30 through 40 and find their average satisfactoin.

Respondents ages 30 through 40 have an average satisfaction rating of about 6.67.

Histograms

I will make a histogram to see how the ages are distributed.

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

Satisfaction Histogram

I will look for peaks and gaps in our satisfaction ratings.

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

##Boxplots

I will use a boxplot to compare satisfactoin ratings across departments.

HR generally has higher satisfaction ratings. Finance also has a low rating that appears as an outlier.

Age by Gender

I will compare the age distribution for each gender category.

The female respondents tend to be younger, and the male group has the widest age range.

Outliers

I will use the IQR method to check whether any satisfaction ratings are unusually high or low.

The lower boundary is 3 and the upper boundary is 11. No satisfacton ratings are outside those boundaries, so there are no outliers.

Department Outliers

I will check for outleirs in each department and see how they affect the average satisfaction.

FInance has an outlier of 4. This lowers its average to 6.4. Without that rating, the avergae would be 7.

library(readxl)
Survey_data <- read_excel("~/Desktop/Math in A.I/Math In Tech/Survey_data.xlsx")
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
HR_ages <- Survey_data$Age[Survey_data$Department == "HR"]
HR_ages
## [1] 25 30 29 27 39
mean(HR_ages)
## [1] 30
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
decade <- floor(AgeSat[, "Age"] / 10) * 10
mean_sat_by_decade <- tapply(AgeSat[, "Satisfaction"], decade, mean)

mean_sat_by_decade
##       20       30       40       50 
## 6.857143 7.000000 6.000000 7.000000
dept_means <- tapply(
  Survey_data$Satisfaction,
  Survey_data$Department,
  mean
)

DeptMat <- matrix(
  dept_means,
  ncol = 1,
  dimnames = list(names(dept_means), "MeanSatisfaction")
)

DeptMat
##           MeanSatisfaction
## Finance                6.4
## HR                     7.8
## IT                     6.4
## Marketing              6.4
DeptTranspose <- t(DeptMat)
colMeans(DeptTranspose)
##   Finance        HR        IT Marketing 
##       6.4       7.8       6.4       6.4
# Check whether Survey_data is a data frame
is.data.frame(Survey_data)
## [1] TRUE
# Find its dimensions: rows, then columns
dim(Survey_data)
## [1] 20  5
# Convert Gender and Department to factors
Survey_data$Gender <- factor(Survey_data$Gender)
Survey_data$Department <- factor(Survey_data$Department)

# Summarize the factor levels and their counts
summary(Survey_data[, c("Gender", "Department")])
##         Gender      Department
##  Female    :8   Finance  :5   
##  Male      :8   HR       :5   
##  Non-binary:4   IT       :5   
##                 Marketing:5
Age30to40 <- Survey_data[
  Survey_data$Age >= 30 & Survey_data$Age <= 40,
]

mean(Age30to40$Satisfaction)
## [1] 6.666667
hist(
  Survey_data$Age,
  main = "Ages of Survey Respondents",
  xlab = "Age"
)

hist(Survey_data$Satisfaction,
  breaks = seq(3.5, 9.5, by = 1),
  main = "Satisfaction Ratings",
  xlab = "Satisfaction Rating",
  ylab = "Count")

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

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

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)
# Create a boxplot; any outliers appear as points
boxplot(
  Satisfaction ~ Department,
  data = Survey_data,
  main = "Satisfaction by Department",
  xlab = "Department",
  ylab = "Satisfaction Rating"
)

# List outlier ratings separately for each department
dept_outliers <- lapply(
  split(Survey_data$Satisfaction, Survey_data$Department),
  function(x) boxplot.stats(x)$out
)

dept_outliers
## $Finance
## [1] 4
## 
## $HR
## numeric(0)
## 
## $IT
## numeric(0)
## 
## $Marketing
## numeric(0)