#Reading the file
sleep<-read.csv("sleep.csv")Case-II: Lifestyle and Sleeping Quality
Test of cae folder
#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
Relation between Occupation and Sleep duration
coljack<- matrix(c("#33ccff","#33ffbb","#33ff33","#99ff33","#ff9933","#ff3333","#ff3399","#ff33ff","#7733ff","#3333ff","#ffff1a"), nrow=11)
sleepocdu<-aggregate( sleep_duration ~ occupation, data = sleep, FUN = mean)
sleepocdu<-cbind(sleepocdu,coljack)
sleepocdu <- sleepocdu[order(-sleepocdu$sleep_duration),]
print(sleepocdu) occupation sleep_duration coljack
3 Engineer 7.987302 #33ff33
4 Lawyer 7.410638 #99ff33
1 Accountant 7.113514 #33ccff
6 Nurse 7.063014 #ff3333
2 Doctor 6.970423 #33ffbb
5 Manager 6.900000 #ff9933
10 Software Engineer 6.750000 #3333ff
11 Teacher 6.690000 #ffff1a
8 Salesperson 6.403125 #ff33ff
9 Scientist 6.000000 #7733ff
7 Sales Representative 5.900000 #ff3399
par(mar = c(5, 4, 4, 2))
bpsd <- barplot(
sleepocdu$sleep_duration, col = sleepocdu$coljack, main = "Sleep duration based on Profession", axis.lty = 0, ylim = c(0, 10), ylab = "Sleep (Hours)", xlab= "Occupation", names.arg = sleepocdu$occupation, las=2
)Engineer is with the highest duration of sleep on average
sleepocqw<-aggregate( sleep_quality ~ occupation, data = sleep, FUN = mean)
sleepocqw<-cbind(sleepocqw,coljack)
sleepocqw <- sleepocqw[order(-sleepocqw$sleep_quality),]
print(sleepocqw) occupation sleep_quality coljack
3 Engineer 8.412698 #33ff33
4 Lawyer 7.893617 #99ff33
1 Accountant 7.891892 #33ccff
6 Nurse 7.369863 #ff3333
5 Manager 7.000000 #ff9933
11 Teacher 6.975000 #ffff1a
2 Doctor 6.647887 #33ffbb
10 Software Engineer 6.500000 #3333ff
8 Salesperson 6.000000 #ff33ff
9 Scientist 5.000000 #7733ff
7 Sales Representative 4.000000 #ff3399
par(mar = c(10, 4, 4, 2))
barplot(sleepocqw$sleep_quality,
names.arg = sleepocqw$occupation,
col = sleepocqw$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<-cbind(sleepocst,coljack)
sleepocst<-sleepocst[order(-sleepocst$stress_level),]
print(sleepocst) occupation stress_level coljack
7 Sales Representative 8.000000 #ff3399
8 Salesperson 7.000000 #ff33ff
9 Scientist 7.000000 #7733ff
2 Doctor 6.732394 #33ffbb
10 Software Engineer 6.000000 #3333ff
6 Nurse 5.547945 #ff3333
4 Lawyer 5.063830 #99ff33
5 Manager 5.000000 #ff9933
1 Accountant 4.594595 #33ccff
11 Teacher 4.525000 #ffff1a
3 Engineer 3.888889 #33ff33
bpst<-barplot(sleepocst$stress_level, col = sleepocst$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 = sleepocst$occupation, srt = 90,adj = 0,cex = 1
)Sleep disorder
table(sleep$sleep_disorder)
Insomnia None Sleep Apnea
77 219 78
Sleep Apnea is most common (excluding no disorder)