Skip to contents

tbl_1var() creates a table giving counts and percentages on one variable.

Usage

tbl_1var(
  data,
  formula,
  digits = 3,
  caption = NULL,
  with_prop = c("no", "yes"),
  na_rm = TRUE
)

Arguments

data

A data frame.

formula

The variable to tabulate. Should be given in formula notation, ~var.

digits

The number of digits to round to. Defaults to 3.

caption

An optional caption for the table. Defaults to NULL, but a sensible default is provided within the function.

with_prop

Should proportions be supplied with instead of percents? Defaults to "no", could also be "yes".

na_rm

Should missing values be removed? Defaults to TRUE.

Value

An object of class flextable. If in an interactive session, the table will be viewable immediately.

Examples

tbl_1var(mtcars, ~gear)
One Way Counts on Variable gear
Missing: 0
NAs Removed: Yes

gear

Count

Percent

3

15

46.875

4

12

37.500

5

5

15.625

Total

32

100.000

tbl_1var(mtcars, ~gear, digits = 2)
One Way Counts on Variable gear
Missing: 0
NAs Removed: Yes

gear

Count

Percent

3

15

46.88

4

12

37.50

5

5

15.62

Total

32

100.00

# Will give an error try(tbl_1var(mtcars, ~Gear)) #> Error in value[[3L]](cond) : #> Could not complete the process. Perhaps you spelled the dataset name or #> a variable name wrong? Hint: R is case-sensitive.