knitr::opts_chunk$set(echo = TRUE) options(knitr.kable.NA = '') library(epiuf) library(kableExtra)
The aim of this function is to describe numeric variables or categorical variables (must be in factor) according or not to another variable in a table. For example : to describe age, sex, vaccination status according to the case or control status.
# create example dataset for testing nbrecords <- 100 df <- data.frame( age = sample(c(0:110,NA), nbrecords, replace = TRUE), sex = factor(sample(c(0,1,NA), nbrecords, replace = TRUE), levels = c(0,1), labels = c("Female", "Male")), fluvaccany = factor(sample(c(0,1,NA), nbrecords, replace = TRUE), levels = c(0,1), labels = c("No", "Yes")), lab_flu = sample(c(0,1), nbrecords, replace = TRUE) ) df$agegp4 <- cut(df$age, c(0, 4, 14, 64, max(df$age, na.rm = T)), include.lowest = TRUE, labels = c("0-4", "5-14", "15-64", "65 +"))
# Test: no labels specified table <- descBy(data = df, vars = c("age", "agegp4", "sex", "fluvaccany")) kable(table) # Example table <- descBy(data = df, vars = c("age", "agegp4", "sex", "fluvaccany"), labels = c("Age", "Age in group", "Sex", "Seasonal influenza vaccination")) kable(table)
# Test: by is not a factor try( table <- descBy(data = df, vars = c("age", "agegp4", "sex"), labels = c("Age", "Age in group", "Sex"), by = "lab_flu") ) # Test: by has not enough categories (minimum is 2) df2 <- subset(df, lab_flu == 0) df2$lab_flu <- factor(df2$lab_flu, levels = c(0), labels = c("Controls")) try ( table <- descBy(data = df2, vars = c("age", "agegp4", "sex"), labels = c("Age", "Age in group", "Sex"), by = "lab_flu") ) # Test: no labels for the variables df$lab_flu <- factor(df$lab_flu, levels = c(0,1), labels = c("Controls", "Cases")) table <- descBy(data = df, vars = c("age", "agegp4", "sex", "fluvaccany"), by = "lab_flu") kable(table) # Example: table <- descBy(data = df, vars = c("age", "agegp4", "sex", "fluvaccany"), labels = c("Age", "Age in group", "Sex", "Seasonal influenza vaccination"), by = "lab_flu") kable(table)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.