In this skills drill, you will be asked to practice the programming skills you have learned so far in order to investigate differences in work absenteeism between cocaine users and non-cocaine users.
The data you are analyzing is from the National Survey of Drug Use and Health (NSDUH), a survey conducted annually since 1971 by the Substance Abuse and Mental Health Services Adminsitration. You will be studying the following two variables:
Load the packages necessary to (1)import, (2)manipulate, and (3)visualize data.
library(readr)
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
Import your data into R
skillsdrilldata<-read_csv('/Users/apple/Downloads/SkillsDrill1Data.csv')
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## .default = col_double(),
## marij_month = col_character(),
## cocaine_month = col_character(),
## pharmamonth = col_character()
## )
## ℹ Use `spec()` for the full column specifications.
Preview the first 6 rows of your data
head(skillsdrilldata)
## # A tibble: 6 x 47
## marij_ever marij_month marij_year cocaine_ever cocaine_month cocaine_year
## <dbl> <chr> <dbl> <dbl> <chr> <dbl>
## 1 1 Used Marij… 1 1 Did not use … 0
## 2 0 Did not us… 0 0 Did not use … 0
## 3 1 Did not us… 0 0 Did not use … 0
## 4 0 Did not us… 0 0 Did not use … 0
## 5 0 Did not us… 0 0 Did not use … 0
## 6 0 Did not us… 0 0 Did not use … 0
## # … with 41 more variables: crack_ever <dbl>, crack_month <dbl>,
## # crack_year <dbl>, heroin_ever <dbl>, heroin_month <dbl>, heroin_year <dbl>,
## # hallucinogen_ever <dbl>, hallucinogen_month <dbl>, hallucinogen_year <dbl>,
## # inhalant_ever <dbl>, inhalant_month <dbl>, inhalant_year <dbl>,
## # meth_ever <dbl>, meth_month <dbl>, meth_year <dbl>, painrelieve_ever <dbl>,
## # painrelieve_month <dbl>, painrelieve_year <dbl>, tranq_ever <dbl>,
## # tranq_month <dbl>, tranq_year <dbl>, stimulant_ever <dbl>,
## # stimulant_month <dbl>, stimulant_year <dbl>, sedative_ever <dbl>,
## # sedative_month <dbl>, sedative_year <dbl>, anydrugever <dbl>,
## # pharmamonth <chr>, nonpharmamonth <dbl>, nonpharmamonth_nomj <dbl>,
## # anydrugmonth <dbl>, anydrugyear <dbl>, anydrugever_nomj <dbl>,
## # anydrugmonth_nomj <dbl>, anydrugyear_nomj <dbl>, countofdrugs_ever <dbl>,
## # countofdrugs_month <dbl>, countofdrugs_year <dbl>, SelectiveLeave <dbl>,
## # SkipSick <dbl>
Select the cocaine_month and SelectiveLeave variables from the data Rename the SelectiveLeave variable to DaysSkippedWork filter to only keep those observations where DaysSkippedWork is less than than 31 Calculate the mean of the DaysSkippedWork
skillsdrilldata %>%
select(cocaine_month, SelectiveLeave)%>%
rename(DaysSkippedWork=SelectiveLeave) %>%
filter(DaysSkippedWork<31)%>%
summarise(MeanDaysSkippedWork=mean(DaysSkippedWork))
## # A tibble: 1 x 1
## MeanDaysSkippedWork
## <dbl>
## 1 0.327
Select the cocaine_month and SelectiveLeave variables from the data Rename the SelectiveLeave variable to DaysSkippedWork filter to only keep those observations where DaysSkippedWork is less than than 31 Calculate the mean of the DaysSkippedWork, by cocaine use (cocaine_month)
skillsdrilldata %>%
select(cocaine_month, SelectiveLeave)%>%
rename(DaysSkippedWork=SelectiveLeave) %>%
filter(DaysSkippedWork<31)%>%
group_by(cocaine_month)%>%
summarise(MeanDaysSkippedWork=mean(DaysSkippedWork))
## # A tibble: 2 x 2
## cocaine_month MeanDaysSkippedWork
## * <chr> <dbl>
## 1 Did not use Cocaine Past 30 days 0.322
## 2 Used Cocaine Past 30 days 0.85
Based on the data, it is evident that those who use cocaine are more likely to miss days at work.
Copy the code from step 5, and paste into this code chunk. Add onto the code to create a visualization which shows a
skillsdrilldata %>%
select(cocaine_month, SelectiveLeave)%>%
rename(DaysSkippedWork=SelectiveLeave) %>%
filter(DaysSkippedWork<31)%>%
group_by(cocaine_month)%>%
summarise(MeanDaysSkippedWork=mean(DaysSkippedWork))%>%
ggplot()+
geom_col(aes(x=cocaine_month, y=MeanDaysSkippedWork, fill=MeanDaysSkippedWork))
(1pt) In the following code chunk, produce the table which compares average absenteeism between marijuana users & non-users (marij_month). Be sure to filter to only keep those observations where DaysSkippedWork is less than than 31.
(0.5pt) In the following code chunk, produce a column chart to visualize the average absenteeism for marijuana users vs. non-users
(0.5pt) Interpret the result of your analysis of the relationship between marijuana use & absenteeism.
Post this to Rpubs & post the Rpubs URL on blackboard