describe_factor: A frequency table for factor variables

View source: R/describe_factor.r

describe_factorR Documentation

A frequency table for factor variables

Description

This function takes a factor variable (numeric variables are transformed into a factor variable) and computes a basic frequency table. Column sums are added automatically, but can be removed if desired. By default, NAs will be removed before computation. Yet, arguments that are usally passed to the table function can be added to also count NAs (useNA = "always").

Usage

describe_factor(x, name = NULL, labels = NULL, cum_percent = FALSE,
  col_sum = TRUE, view = FALSE, ...)

Arguments

x

A numeric or factor variable.

name

A name for the first colum (if not specified, it remains the variable code).

labels

If you are evaluating a numeric vector, the function will transform it into a factor variable. You can specify custom labels for the factor levels here.

cum_percent

Logical value indicating whether cumulative percent should also be returned.

col_sum

Logical value indicating whether column sums should be returned.

view

Logical value indicating whether the full data frame should be printed (defaults to FALSE).

...

Further arguments that can be passed to table. This has been primarily included to allow the counting of NAs. If desired, simply add the following argument: useNA = "always".

Value

The function describe_factor() returns a contigency table with relative and absolute values and an object of class tbl_df (a data frame).

Examples

# Standard example
d <- mtcars
describe_factor(d$cyl, name = "No. of cylinders")
describe_factor(d$mpg, view = TRUE)

# Example with missing values
d$cyl[3] <- NA # Creating a missing value
describe_factor(d$cyl, 
                name = "No. of cylinders", 
                cum_percent = TRUE,
                useNA = "always")

masurp/pmmisc documentation built on May 3, 2024, 7:13 p.m.