tab_var: Tabulate a variable

Description Usage Arguments Value Author(s) Examples

View source: R/tab_var.R

Description

Tabulate a variable

Usage

1
2
3
4
tab_var(data, var = NULL, by = NULL, complete = FALSE,
  arrange.factor.by = "value", show.percentage = "column",
  show.na.percentage = TRUE, n.decimals = 0, trunc.length = 60,
  plotit = FALSE)

Arguments

data

a data.frame

var

a character specifying the variable to tabulate, if set to NULL the first variable will be used

by

a character specifying the variable to stratify by, if set to NULL no stratification will be used

complete

a logical specifying whether to use all levels for factor variables

arrange.factor.by

a character with value either "level" or "value" describing how a factor variable should be ordered

show.percentage

a character either 'column' or 'row' or NULL to indicate whether to show percentages in output

show.na.percentage

a logical specifying whether to show percentages for NA values

n.decimals

a numeric specifying the number of decimal places to show

trunc.length

a numeric specifying the maximum character length to be shown in the output

plotit

a logical specifying whether to plot the output

Value

either (1) a data.frame or (2) a list including a data.frame and a ggplot figure

Author(s)

Daniel Gardiner (daniel.gardiner@phe.gov.uk)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# set dummy data

set.seed(4)

data = data.frame(onset.date = sample(seq(as.Date("2017-01-01"), as.Date("2018-06-01"), 1), 200, replace = TRUE),
                 sex = factor(c("M", "M", "F", NA, NA), c("F", "M", "Unk")),
                 conf = sample(c("Confirmed", "Probable", "Probable"), 200, replace = TRUE),
                 status = sample(c("Student", "Staff", NA), 200, replace = TRUE),
                 geog = sample(c("South", "North", NA), 200, replace = TRUE))

# apply function

tab_var(data, var = "sex")

# using dplyr syntax

data %>%
 tab_var(var = "sex", by = "conf")

data %>%
 tab_var("geog", "conf", show.percentage = NULL)

data %>%
 tab_var("geog", "conf",
          show.percentage = "row",
          n.decimals = 2,
          plotit = TRUE)

DanielGardiner/EpiFunc documentation built on July 25, 2019, 10:53 p.m.