Learn R Programming

summarytools (version 0.9.2)

freq: Frequency Tables for Factors and Other Discrete Data

Description

Displays weighted or unweighted frequencies, including <NA> counts and proportions.

Usage

freq(x, round.digits = st_options("round.digits"), order = "default",
  style = st_options("style"), plain.ascii = st_options("plain.ascii"),
  justify = "default", totals = st_options("freq.totals"),
  report.nas = st_options("freq.report.nas"), missing = "",
  display.type = TRUE, display.labels = st_options("display.labels"),
  headings = st_options("headings"), weights = NA,
  rescale.weights = FALSE, ...)

Arguments

x

Factor or vector

round.digits

Number of significant digits to display. Defaults to 2 and can be set globally; see st_options.

order

Ordering of rows in frequency table; “names” (default for non-factors), “levels” (default for factors), or “freq” (from most frequent to less frequent).

style

Style to be used by pander when rendering output table; One of “simple” (default), “grid”, or “rmarkdown” This option can be set globally; see st_options.

plain.ascii

Logical. pander argument; when TRUE, no markup characters will be used (useful when printing to console). Defaults to TRUE unless style = 'rmarkdown', in which case it will be set to FALSE automatically. To change the default value globally, see st_options.

justify

String indicating alignment of columns. By default (“default”), “right” is used for text tables and “center” is used for html tables. You can force it to one of “left”, “center”, or “right”.

totals

Logical. Set to FALSE to hide totals from results. To change this value globally, see st_options.

report.nas

Logical. Set to FALSE to turn off reporting of missing values. To change this default value globally, see st_options.

missing

Characters to display in NA cells. Defaults to “”.

display.type

Logical. Should variable type be displayed? Default is TRUE.

display.labels

Logical. Should variable / data frame labels be displayed? Default is TRUE. To change this default value globally, see st_options.

headings

Logical. Set to FALSE to omit heading section. Can be set globally via st_options.

weights

Vector of weights; must be of the same length as x.

rescale.weights

Logical parameter. When set to TRUE, the total count will be the same as the unweighted x. FALSE by default.

Additional arguments passed to pander.

Value

A frequency table of class matrix and summarytools with added attributes used by print method.

Details

The default plain.ascii = TRUE option is there to make results appear cleaner in the console. To avoid rmarkdown rendering problems, this option is automatically set to FALSE whenever style = "rmarkdown" (unless plain.ascii = TRUE is made explicit in the function call).

See Also

table

Examples

Run this code
# NOT RUN {
data(tobacco)
freq(tobacco$gender)
freq(tobacco$gender, totals = FALSE)

# Ignore NA's, don't show totals, omit headings
freq(tobacco$gender, report.nas = FALSE, totals = FALSE, headings = FALSE)

# In .Rmd documents, use the two following arguments, minimally
freq(tobacco$gender, style="rmarkdown", plain.ascii = FALSE)

# Grouped Frequencies
with(tobacco, stby(diseased, smoker, freq))
(fr_smoker_by_gender <- with(tobacco, stby(smoker, gender, freq)))

# Print html Source
print(fr_smoker_by_gender, method = "render", footnote = NA)

# }
# NOT RUN {
# Display rendered html results in RStudio's Viewer
# notice 'view()' is NOT written with capital V
# If working outside RStudio, Web browser is used instead
# A temporary file is stored in temp dir
view(fr_smoker_by_gender)

# Display rendered html results in default Web browser
# A temporary file is stored in temp dir here too
print(fr_smoker_by_gender, method = "browser")

# Write results to text file (.txt, .md, .Rmd) or html file (.html)
print(fr_smoker_by_gender, method = "render", file = "fr_smoker_by_gender.md)
print(fr_smoker_by_gender, method = "render", file = "fr_smoker_by_gender.html)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab