View source: R/describe_factor.r
describe_factor | R Documentation |
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").
describe_factor(x, name = NULL, labels = NULL, cum_percent = FALSE,
col_sum = TRUE, view = FALSE, ...)
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 |
The function describe_factor()
returns a contigency table with relative and absolute values and an object of class tbl_df
(a data frame).
# 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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.