knitr::opts_chunk$set(echo = TRUE, tidy.opts=list(width.cutoff=80), tidy=TRUE, comment=NA) options(width=80, max.print=1000)
freqlist()
is a function meant to produce output similar to SAS's PROC FREQ
procedure when using the /list
option of the TABLE
statement.
freqlist()
provides options for handling missing or sparse data and can provide cumulative counts and percentages based on subgroups.
It depends on the knitr
package for printing.
require(arsenal)
For our examples, we'll load the mockstudy
data included with this package and use it to create a basic table.
Because they have fewer levels, for brevity, we'll use the variables arm, sex, and mdquality.s to create the example table.
We'll retain NAs in the table creation.
See the appendix for notes regarding default NA handling and other useful information regarding tables in R.
# load the data data(mockstudy) # retain NAs when creating the table using the useNA argument tab.ex <- table(mockstudy[, c("arm", "sex", "mdquality.s")], useNA="ifany")
freqlist
objectThe freqlist()
function is an S3 generic (with methods for tables and formulas) which returns an object of class "freqlist"
.
example1 <- freqlist(tab.ex) str(example1) # view the data frame portion of freqlist output head(as.data.frame(example1)) ## or use as.data.frame(example1)
summary()
The summary
method for freqlist()
relies on the kable()
function (in the knitr
package) for printing.
knitr::kable()
converts the output to markdown which can be printed in the console or easily rendered in
Word, PDF, or HTML documents.
Note that you must supply results="asis"
to properly format the markdown output.
summary(example1)
You can print a title for the table using the title=
argument.
summary(example1, title="Basic freqlist output")
You can also easily pull out the freqlist
data frame for more complicated formatting or manipulation
(e.g. with another function such as xtable()
or pander()
) using as.data.frame(summary())
:
head(as.data.frame(summary(example1)))
freqlist
Instead of passing a pre-computed table to freqlist()
, you can instead pass a formula, which will be
in turn passed to the xtabs()
function. Additional freqlist()
arguments are passed through the ...
to the freqlist()
table method.
Note that freqlist()
sets the addNA=TRUE
argument by default:
summary(freqlist(~ arm + sex + mdquality.s, data = mockstudy))
One can also set NAs to an explicit value using includeNA()
.
summary(freqlist(~ arm + sex + includeNA(mdquality.s, "Missing"), data = mockstudy))
In fact, since xtabs()
allows for left-hand-side weights, so does freqlist()
!
mockstudy$weights <- c(10000, rep(1, nrow(mockstudy) - 1)) summary(freqlist(weights ~ arm + sex + addNA(mdquality.s), data = mockstudy))
You can also specify multiple weights:
mockstudy$weights2 <- c(rep(1, nrow(mockstudy) - 1), 10000) summary(freqlist(list(weights, weights2) ~ arm + sex + addNA(mdquality.s), data = mockstudy))
The digits.pct=
argument takes a single numeric value and controls the number of digits of percentages in the output.
The digits.count=
argument takes a similar argument and controls the number of digits of the count columns.
The labelTranslations=
argument is a named character vector or list.
Both options are applied in the following example.
example2 <- freqlist(tab.ex, labelTranslations = c(arm = "Treatment Arm", sex = "Gender", mdquality.s = "LASA QOL"), digits.pct = 1, digits.count = 1) summary(example2)
The sparse=
argument takes a single logical value as input. The default option is FALSE
.
If set to TRUE
, the sparse option will include combinations with frequencies of zero in the list of results.
As our initial table did not have any such levels, we create a second table to use in our example.
summary(freqlist(~ race + sex + arm, data = mockstudy, sparse = TRUE, digits.pct=1))
The various na.options=
allow you to include or exclude data with missing values for one or more factor
levels in the counts and percentages, as well as show the missing data but exclude it from the cumulative
counts and percentages. The default option is to include all combinations with missing values.
summary(freqlist(tab.ex, na.options="include")) summary(freqlist(tab.ex, na.options="showexclude")) summary(freqlist(tab.ex, na.options="remove"))
The strata=
argument internally subsets the data by the specified factor prior to calculating cumulative counts and percentages.
By default, when used each subset will print in a separate table. Using the single = TRUE
option when printing will collapse
the subsetted result into a single table.
example3 <- freqlist(tab.ex, strata = c("arm","sex")) summary(example3) #using the single = TRUE argument will collapse results into a single table for printing summary(example3, single = TRUE)
head()
and sort()
)You can now sort freqlist()
objects, and, by taking the head()
of the summary, output the most common frequencies. This looks
the prettiest with dupLabels=TRUE
.
head(summary(sort(example1, decreasing = TRUE), dupLabels = TRUE))
labs <- c(arm = "Arm", sex = "Sex", mdquality.s = "QOL", freqPercent = "%") labels(example1) <- labs summary(example1)
You can also supply labelTranslations=
to summary()
.
summary(example1, labelTranslations = labs)
xtable()
to format and print freqlist()
resultsFair warning: xtable()
has kind of a steep learning curve. These examples are given without explanation, for more advanced users.
require(xtable) # set up custom function for xtable text italic <- function(x) paste0('<i>', x, '</i>') xftbl <- xtable(as.data.frame(summary(example1)), caption = "xtable formatted output of freqlist data frame", align="|r|r|r|r|c|c|c|r|") # change the column names names(xftbl)[1:3] <- c("Arm", "Gender", "LASA QOL") print(xftbl, sanitize.colnames.function = italic, include.rownames = FALSE, type = "html", comment = FALSE)
freqlist
in bookdownSince the backbone of freqlist()
is knitr::kable()
, tables still render well in bookdown. However, print.summary.freqlist()
doesn't use
the caption=
argument of kable()
, so some tables may not have a properly numbered caption. To fix this, use the method described
on the bookdown site to give the table a tag/ID.
summary(freqlist(~ sex + age, data = mockstudy), title="(\\#tab:mytableby) Caption here")
There are several widely used options for basic tables in R. The table()
function in base R is probably the most common;
by default it excludes NA values. You can change NA handling in base::table()
using the useNA=
or exclude=
arguments.
# base table default removes NAs tab.d1 <- base::table(mockstudy[, c("arm", "sex", "mdquality.s")], useNA="ifany") tab.d1
xtabs()
is similar to table()
, but uses a formula-based syntax. However, NAs must be explicitly added to each factor
using the addNA()
function or using the argument addNA = TRUE
.
# without specifying addNA tab.d2 <- xtabs(formula = ~ arm + sex + mdquality.s, data = mockstudy) tab.d2 # now with addNA tab.d3 <- xtabs(~ arm + sex + addNA(mdquality.s), data = mockstudy) tab.d3
Since the formula method of freqlist()
uses xtabs()
, NAs should be treated in the same way.
includeNA()
can also be helpful here for setting explicit NA values.
Supplying a data.frame to the table()
function without giving columns individually will create a contingency table
using all variables in the data.frame.
However, if the columns of a data.frame or matrix are supplied separately (i.e., as vectors), column names will not be preserved.
# providing variables separately (as vectors) drops column names table(mockstudy$arm, mockstudy$sex, mockstudy$mdquality.s)
If desired, you can use the dnn=
argument to pass variable names.
# add the column name labels back using dnn option in base::table table(mockstudy$arm, mockstudy$sex, mockstudy$mdquality.s, dnn=c("Arm", "Sex", "QOL"))
You can also name the arguments to table()
:
table(Arm = mockstudy$arm, Sex = mockstudy$sex, QOL = mockstudy$mdquality.s)
If using freqlist()
, you can provide the labels directly to freqlist()
or to summary()
using labelTranslations=
.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.