How to access a column in R:
We have the dataset Satisfaction_Set_1. We want to extract the Satisfaction data as our main column.
To do so:
Sat<-Satisfaction_Set_1_$Satisfaction
mean(Sat)
[1] 6.75
median(Sat)
[1] 7
Now, we would like to create a specific column for the ages of the HR department:
hr_ages<-Satisfaction_Set_1_$Age[Satisfaction_Set_1_$Department == "HR"]
Next, we would like to convert the Age and Satisfaction columns into a matrix:
age_satisfaction_matrix <- as.matrix(
Satisfaction_Set_1_[, c("Age", "Satisfaction")]
)
summary(age_satisfaction_matrix)
Age Satisfaction
Min. :22.00 Min. :4.00
1st Qu.:28.00 1st Qu.:6.00
Median :32.00 Median :7.00
Mean :33.95 Mean :6.75
3rd Qu.:39.25 3rd Qu.:8.00
Max. :50.00 Max. :9.00
Our next step will be to create a matric where each row now will represent a department and their respective means:
department_means <- tapply(
Satisfaction_Set_1_$Satisfaction,
Satisfaction_Set_1_$Department,
mean
)
We want to assign this data to a data frame. We also want to summarize the Gender and Department columns:
View(Satisfaction_Set_1_)
dim(Satisfaction_Set_1_)
[1] 20 5
summary(Satisfaction_Set_1_[, c("Gender", "Department")])
Gender Department
Length :20 Length :20
N.unique : 3 N.unique : 4
N.blank : 0 N.blank : 0
Min.nchar: 4 Min.nchar: 2
Max.nchar:10 Max.nchar: 9
To filter the dataset by 30 and 40-year-olds, we will use the following:
age_30_40 <- subset(Satisfaction_Set_1_, Age >= 30 & Age <= 40)
print(age_30_40)
# A tibble: 9 × 5
RespondentID Age Gender Satisfaction Department
<dbl> <dbl> <chr> <dbl> <chr>
1 2 34 Male 6 IT
2 5 30 Female 9 HR
3 7 40 Male 4 Finance
4 8 35 Non-binary 6 Marketing
5 11 31 Female 9 Marketing
6 12 38 Male 6 IT
7 16 33 Female 8 IT
8 17 39 Male 7 HR
9 20 31 Male 5 IT
To create a histogram of the Age column:
hist(Satisfaction_Set_1_$Age)
Now, we want to plot a histogram of the Satisfaction ratings:
hist(Satisfaction_Set_1_$Satisfaction)
Now, we will create a boxplot reflectiong the Satisfaction ratings by Department:
boxplot(Satisfaction~Department, data=Satisfaction_Set_1_)
Next, we want to generate a boxplot of Age by Gender:
boxplot(Age~Gender, data=Satisfaction_Set_1_)
We now want to identify the outliers, if any, in the Satisfaction ratings using the IQR method:
Q1<-quantile(Satisfaction_Set_1_$Satisfaction, 0.25)
Q3<-quantile(Satisfaction_Set_1_$Satisfaction, 0.75)
IQR_value<-IQR(Satisfaction_Set_1_$Satisfaction)
Finally, we want to create a boxplot of Satisfaction ratings by Department, with identification if there are any departments with outliers in the Satisfaction ratings:
boxplot(Satisfaction ~ Department, data=Satisfaction_Set_1_)
The outlier is identified by the round point outside of the boxplot boundaries. For Finance, this is an outlier value of 4.