Step 1 Loading required packages
library(mosaic)
library(mosaicData)
library(dplyr)
library(kableExtra)
Step2 Print first 5 rows and first 5 columns of data
Note I am using one of the several formats avaible at https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html
dt<-HELPrct[1:5, 1:5]
dt %>%
kbl(caption = "Booktabs style table for HELPrct") %>%
kable_classic(full_width = F, html_font = "Cambria")
Booktabs style table for HELPrct
age
|
anysubstatus
|
anysub
|
cesd
|
d1
|
37
|
1
|
yes
|
49
|
3
|
37
|
1
|
yes
|
30
|
22
|
26
|
1
|
yes
|
39
|
0
|
39
|
1
|
yes
|
15
|
2
|
32
|
1
|
yes
|
39
|
12
|
Step 3 Add an additional column (secondary variable created from primary variable) using avg_drinks column, call it drinks_binary. Anyone with >=5 drinks == heavy drinker, otherwise == not heavy drinker. Use tally to count how many in each category.
HELPrct$drinks_binary[HELPrct$avg_drinks >= 5] <- "Heavy Drinker"
HELPrct$drinks_binary[HELPrct$avg_drinks < 5] <- "Not a Heavy Drinker"
count.drinker<-tally(~drinks_binary, data=HELPrct)
kbl(count.drinker)
drinks_binary
|
Freq
|
Heavy Drinker
|
313
|
Not a Heavy Drinker
|
140
|
Step4 Create an additional race column, which merges racegroups into three categories only, white, black and others (others +Hispanics).
mydata <- HELPrct
race.1 <- recode(mydata$racegrp, "hispanic" = "other")
mydata.1 <- mutate(mydata, race.1)
dt1<-mydata.1[1:5, c(1,23, 32)]
dt1 %>%
kbl(caption = "HELPrct with new race column") %>%
kable_classic(full_width = F, html_font = "Cambria")
HELPrct with new race column
age
|
racegrp
|
race.1
|
37
|
black
|
black
|
37
|
white
|
white
|
26
|
black
|
black
|
39
|
white
|
white
|
32
|
black
|
black
|