data %>%
tabyl(col_1)Create a one-way frequency table
You want to create a one-way frequency table from a categorical column in a data frame. One-way frequency tables show the distribution of a categorical variable.
Step 1 - Start with the data frame.
Step 2 - Ensure the data frame is tidy. The variable you want to appear in the frequency table should be in its own column.
Step 3 - Then use janitor::tabyl() to create the frequency table.
Step 4 (Optional) - Format the percent column using janitor::adorn_pct_formatting().
data %>%
tabyl(col_1) %>%
adorn_pct_formatting()Example
fda_adverse describes adverse events that have been reported to the FDA for various drugs. We are interested in the outcome column, which describes the long-term outcome for the individuals who reported each event.
To begin, we first load the janitor package. Then we pass the data frame fda_adverse to tabyl() and then call our variable of interest: outcome.
library(janitor)
fda_adverse %>%
tabyl(outcome) outcome n percent
fatal 277 0.04804857
not recovered 966 0.16756288
recovered 2020 0.35039029
recovered with sequelae 82 0.01422376
recovering 980 0.16999133
unknown 1440 0.24978317
Finally, we format the percent column so the values look like percentages.
fda_adverse %>%
tabyl(outcome) %>%
adorn_pct_formatting() outcome n percent
fatal 277 4.8%
not recovered 966 16.8%
recovered 2020 35.0%
recovered with sequelae 82 1.4%
recovering 980 17.0%
unknown 1440 25.0%
From this table we can see how many adverse events fell into each outcome category. For example, the majority of adverse events have the outcome of recovered, with \(2020\) events, which is \(35\)% of the event outcomes.
Frequency tables and SAS
Frequency tables are obtained in SAS using the FREQ procedure with the TABLES statement.
In SAS:
PROC FREQ DATA = data;
TABLES col_1 / NOCUM;
RUN;In R:
data %>%
tabyl(col_1) %>%
adorn_pct_formatting()