Setup

setwd("D:/stat")
getwd()
## [1] "D:/stat"

load(“brfss2013.Rdata”) ### Load packages

library(ggplot2)
library(dplyr)

Load data

load("brfss2013.Rdata")

Refer to the provided data in our google classroom.

##1. Using the variable “sleptim1” and “marital”, determine the following:

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

brfss2013%>% 
  group_by(sleptim1)%>%
  filter(is.na(sleptim1))%>%
  summarize(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%>%
  filter(sleptim1<=c(5))%>%
  group_by(sleptim1)%>%
  summarise(n=n())
## # A tibble: 6 × 2
##   sleptim1     n
##      <int> <int>
## 1        0     1
## 2        1   228
## 3        2  1076
## 4        3  3496
## 5        4 14261
## 6        5 33436
SLEEP<-brfss2013%>%
  filter(sleptim1<=c(5))%>%
  group_by(sleptim1)%>%
  summarise(n=n())
sum(SLEEP$n)
## [1] 52498

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

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

#at 1.4 number of observations having at least 11 hours of sleep and

brfss2013%>%
  filter(sleptim1>=c(11))%>%
  group_by(sleptim1)%>%
  summarise(n=n())
## # A tibble: 16 × 2
##    sleptim1     n
##       <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
## 15      103     1
## 16      450     1
SLEEP2<-brfss2013%>%
  filter(sleptim1>=c(11))%>%
  group_by(sleptim1)%>%
  summarise(n=n())
sum(SLEEP2$n)
## [1] 6220

#1.5 number of observations having at most 5 hours of sleep that are married.

brfss2013%>%
  filter(sleptim1<=c(5), marital=="Married")%>%
  group_by(sleptim1,marital)%>%
  summarise(n=n())
## `summarise()` has grouped output by 'sleptim1'. You can override using the
## `.groups` argument.
## # A tibble: 5 × 3
## # Groups:   sleptim1 [5]
##   sleptim1 marital     n
##      <int> <fct>   <int>
## 1        1 Married    61
## 2        2 Married   302
## 3        3 Married  1120
## 4        4 Married  5452
## 5        5 Married 14482
SLEEP3<-brfss2013%>%
  filter(sleptim1<=c(5), marital=="Married")%>%
  group_by(sleptim1)%>%
  summarise(n=n())
sum(SLEEP3$n)
## [1] 21417