#Reading the file
sleep<-read.csv("sleep.csv")
coljack<-c(
"#33ccff",
"#33ffbb",
"#33ff33",
"#99ff33",
"#ff9933",
"#ff3333",
"#ff3399",
"#ff33ff",
"#7733ff",
"#3333ff",
"#ffff1a"
)Case-II: Lifestyle and Sleeping Quality
Part A General Summary
Hi Jacskon
#Types of Jobs at a study
unique(sleep$occupation) [1] "Software Engineer" "Doctor" "Sales Representative"
[4] "Teacher" "Nurse" "Engineer"
[7] "Accountant" "Scientist" "Lawyer"
[10] "Salesperson" "Manager"
#Types of Sleep disorders in the study
unique(sleep$sleep_disorder)[1] "None" "Sleep Apnea" "Insomnia"
#Range of sleep quality
sort(unique(sleep$sleep_quality))[1] 4 5 6 7 8 9
#Sample size
print(sample_size<-length(sleep$gender))[1] 374
#Mean of sleep time
print(mean_sleepdur<-mean(sleep$sleep_duration))[1] 7.132086
boxplot(sleep$age,horizontal =TRUE,col="lightblue",main="Age of sample")sleepocdu<-aggregate( sleep_duration ~ occupation, data = sleep, FUN = mean)
sleepocdu <- sleepocdu[order(-sleepocdu$sleep_duration),]
print(sleepocdu) occupation sleep_duration
3 Engineer 7.987302
4 Lawyer 7.410638
1 Accountant 7.113514
6 Nurse 7.063014
2 Doctor 6.970423
5 Manager 6.900000
10 Software Engineer 6.750000
11 Teacher 6.690000
8 Salesperson 6.403125
9 Scientist 6.000000
7 Sales Representative 5.900000
par(mar = c(5, 4, 4, 2)) # smaller bottom margin now
bpsd <- barplot(
sleepocdu$sleep_duration,
col = coljack
,
main = "Sleep duration based on Profession",
axis.lty = 0,
ylim = c(0, 10),
ylab = "Sleep (Hours)",
xlab= "Occupation"
)
text(
x = bpsd,
y = sleepocdu$sleep_duration + 0.3,
labels = sleepocdu$occupation,
srt = 90,
adj = 0,
cex = 1
)Engineer is with the highest duration of sleep on average
sleepocqw<-aggregate( sleep_quality ~ occupation, data = sleep, FUN = mean)
sleepocqw <- sleepocqw[order(-sleepocqw$sleep_quality),]
print(sleepocqw) occupation sleep_quality
3 Engineer 8.412698
4 Lawyer 7.893617
1 Accountant 7.891892
6 Nurse 7.369863
5 Manager 7.000000
11 Teacher 6.975000
2 Doctor 6.647887
10 Software Engineer 6.500000
8 Salesperson 6.000000
9 Scientist 5.000000
7 Sales Representative 4.000000
par(mar = c(10, 4, 4, 2))
barplot(sleepocqw$sleep_quality,
names.arg = sleepocqw$occupation,
col = coljack
,
main = "Sleep quality based on Profession",
axis.lty = 0,
las=2,
ylim = c(0,10),
ylab = "Sleep quality (1-10)"
)sleepocst<-aggregate(stress_level ~ occupation, data = sleep, FUN = mean )
sleepocst<-sleepocst[order(-sleepocst$stress_level),]
print(sleepocst) occupation stress_level
7 Sales Representative 8.000000
8 Salesperson 7.000000
9 Scientist 7.000000
2 Doctor 6.732394
10 Software Engineer 6.000000
6 Nurse 5.547945
4 Lawyer 5.063830
5 Manager 5.000000
1 Accountant 4.594595
11 Teacher 4.525000
3 Engineer 3.888889
bpst<-barplot(sleepocst$stress_level,
col = coljack,
main = "Occupation and stress level",
ylab = "Stress level (1-10)",
ylim = c(0,10),
)
text(
x = bpst,
y = sleepocst$stress_level + 0.3,
labels = sleepocdu$occupation,
srt = 90,
adj = 0,
cex = 1
)table(sleep$sleep_disorder)
Insomnia None Sleep Apnea
77 219 78
Sleep Apnea is most common (excluding no illness)