Description Usage Arguments Details Value See Also Examples
Describe data sets with multiple variable types effectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | describe_all(
data,
digits = 2,
include_NAcat = TRUE,
max_levels = 10,
include_numeric = FALSE,
NAcat_include = NULL,
sort_by_freq = TRUE,
...
)
describe_all_num(data, digits = 2, ...)
describe_all_cat(
data,
digits = 2,
include_NAcat = TRUE,
max_levels = 10,
include_numeric = FALSE,
sort_by_freq = TRUE,
as_ordered = FALSE
)
|
data |
The dataset, of class data.frame. |
digits |
See [base::round()]. Default is 2, which for categorical is applied to the proportion (i.e. before converting to percentage). |
include_NAcat |
Include NA values as categorical levels? Default is
|
max_levels |
The maximum number of levels you want to display for categorical variables. Default is 10. |
include_numeric |
For categorical summary, also include numeric
variables with unique values fewer or equal to |
NAcat_include |
Deprecated alias of |
sort_by_freq |
Sort categorical levels by frequency? Default is
|
... |
Additional arguments passed to |
as_ordered |
Return the categorical results with the levels as ordered. See details and example. |
This function comes out of my frustrations from various data set summaries either being inadequate for my needs, too 'busy' with output, or unable to deal well with mixed data types.
Numeric data is treated separately from categorical, and provides the same
information as in num_summary
.
Categorical variables are defined as those with class 'character', 'factor',
'logical', 'ordered', combined with include_numeric
. They are are
summarized with frequencies and percentages. For empty categorical
variables (e.g. after a subset), a warning is thrown. Note that max_levels
is used with top_n, and so will return additional values when
there are ties.
The as_ordered
argument is to get around the notorious alphabetical
ordering of ggplot. It returns a data.frame where the 'data' column
contains the frequency information of the categorical levels, while leaving
the levels in order (e.g. decreasing if sort_by_freq
was
TRUE
). This way you can directly plot the result in the manner
you've actually requested. See the example.
The functions describe_all_num
and describe_all_cat
will
provide only numeric or only categorical data summaries respectively.
describeAll
is a deprecated alias.
A list with two elements of summaries for numeric and other variables respectively. Or the contents of those elements if the type-specific functions are used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | library(tidyext); library(dplyr)
X = data.frame(f1 =gl(2, 1, 20, labels=c('A', 'B')),
f2=gl(2, 2, 20, labels=c('X', 'Q')))
X = X %>% mutate(bin1 = rbinom(20, 1, p=.5),
logic1 = sample(c(TRUE, FALSE), 20, replace = TRUE),
num1 = rnorm(20),
num2 = rpois(20, 5),
char1 = sample(letters, 20, replace = TRUE))
describe_all(X)
describe_all(data.frame(x=factor(1:7)), digits=5)
describe_all(mtcars, digits=5, include_numeric=TRUE, max_levels=3)
library(ggplot2)
freqs = describe_all_cat(mtcars,
include_numeric=TRUE,
max_levels=3,
as_ordered = TRUE)
freqs %>%
filter(Variable == 'cyl') %>%
tidyr::unnest() %>%
ggplot(aes(x=Group, y=`%`)) +
geom_point(size = 10)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.