Setup

getwd()
## [1] "C:/Quiz"

Load packages

library(ggplot2)
library(dplyr)

Load data

load("brfss2013.RData")
  1. Using the variable “sleptim1” and “marital”, determine the following:

1.1 number of observations that are “NA” in the variable “sleptim1”

total_obs <- nrow(brfss2013) 
  
brfss2013 %>%
  group_by(sleptim1) %>%
  filter(is.na(sleptim1) )%>%
  summarise(count=n())
## # A tibble: 1 × 2
##   sleptim1 count
##      <int> <int>
## 1       NA  7387

1.2 number of observations having at most 5 hours of sleep;

brfss2013 %>%
  group_by(sleptim1) %>%
  filter(sleptim1 <= 5)%>%
  summarise(frequency=n())
## # A tibble: 6 × 2
##   sleptim1 frequency
##      <int>     <int>
## 1        0         1
## 2        1       228
## 3        2      1076
## 4        3      3496
## 5        4     14261
## 6        5     33436
x <- brfss2013 %>%
  group_by(sleptim1) %>%
  filter(sleptim1 <= 5)%>%
  summarise(frequency=n())
sum(x$frequency)
## [1] 52498

1.3 number of observations having more than 5 hours of sleep but less than 11 hours of sleep;

xx <- brfss2013 %>%
  group_by(sleptim1) %>%
  filter(sleptim1 > 5)%>%
  filter(sleptim1 < 11)%>%
  summarise(frequency=n())
xx
## # A tibble: 5 × 2
##   sleptim1 frequency
##      <int>     <int>
## 1        6    106197
## 2        7    142469
## 3        8    141102
## 4        9     23800
## 5       10     12102
sum(xx$frequency)
## [1] 425670

1.4 number of observations having at least 11 hours of sleep;

xxx <- brfss2013 %>%
  group_by(sleptim1) %>%
  filter(sleptim1 >= 11)%>%
  filter(sleptim1 <= 24)%>%
  summarise(frequency=n())
xxx
## # A tibble: 14 × 2
##    sleptim1 frequency
##       <int>     <int>
##  1       11       833
##  2       12      3675
##  3       13       199
##  4       14       447
##  5       15       367
##  6       16       369
##  7       17        35
##  8       18       164
##  9       19        13
## 10       20        64
## 11       21         3
## 12       22        10
## 13       23         4
## 14       24        35
sum(xxx$frequency)
## [1] 6218

1.5 numbers of observation having at most 5 hours of sleep that are married;

xxxx <-  brfss2013 %>%
  group_by(sleptim1) %>%
  filter(sleptim1 <= 5, marital == "Married")%>%
  summarise(frequency=n())
xxxx
## # A tibble: 5 × 2
##   sleptim1 frequency
##      <int>     <int>
## 1        1        61
## 2        2       302
## 3        3      1120
## 4        4      5452
## 5        5     14482
sum(xxxx$frequency)
## [1] 21417