Setup

setwd("E:/STAT 50")
getwd()
## [1] "E:/STAT 50"

Load packages

library(ggplot2)
library(dplyr)

Load data

load("brfss2013.RData")

STAT 50 Quiz 2

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)) %>%
  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

sleepA<-brfss2013 %>%
  group_by(sleptim1) %>%
  filter(sleptim1 <=5) %>%
  summarise(count=n())

sum(sleepA$count)
## [1] 52498

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

sleepF<-brfss2013 %>%
  group_by(sleptim1) %>%
  filter(sleptim1 >5, sleptim1 <11) %>%
  summarise(count=n())

sum(sleepF$count)
## [1] 425670

1.4 number of observations having at least 11 hours of sleep

sleepB<-brfss2013 %>%
  group_by(sleptim1) %>%
  filter(sleptim1 >=11) %>%
  summarise(count=n())

sum(sleepB$count)
## [1] 6220

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

sleepM<-brfss2013 %>%
  group_by(sleptim1) %>%
  filter(sleptim1 <=5, marital == "Married") %>%
  summarise(count=n())
  
sum(sleepM$count)
## [1] 21417