Generate and format frequency tables from a variable or a table, with percentages and formatting options.

freq(
  x,
  digits = 1,
  cum = FALSE,
  total = FALSE,
  exclude = NULL,
  sort = "",
  valid = !(NA %in% exclude),
  levels = c("prefixed", "labels", "values"),
  na.last = TRUE
)

Arguments

x

either a vector to be tabulated, or a table object

digits

number of digits to keep for the percentages

cum

if TRUE, display cumulative percentages

total

if TRUE, add a final row with totals

exclude

vector of values to exclude from the tabulation (if x is a vector)

sort

if specified, allow to sort the table by increasing ("inc") or decreasing ("dec") frequencies

valid

if TRUE, display valid percentages

levels

the desired levels for the factor in case of labelled vector (labelled package must be installed): "labels" for value labels, "values" for values or "prefixed" for labels prefixed with values

na.last

if TRUE, NA values are always be last table row

Value

The result is an object of class data.frame.

See also

Examples

# factor
data(hdv2003)
freq(hdv2003$qualif)
#>                            n    % val%
#> Ouvrier specialise       203 10.2 12.3
#> Ouvrier qualifie         292 14.6 17.7
#> Technicien                86  4.3  5.2
#> Profession intermediaire 160  8.0  9.7
#> Cadre                    260 13.0 15.7
#> Employe                  594 29.7 35.9
#> Autre                     58  2.9  3.5
#> NA                       347 17.3   NA
freq(hdv2003$qualif, cum = TRUE, total = TRUE)
#>                             n     %  val%  %cum val%cum
#> Ouvrier specialise        203  10.2  12.3  10.2    12.3
#> Ouvrier qualifie          292  14.6  17.7  24.8    29.9
#> Technicien                 86   4.3   5.2  29.0    35.1
#> Profession intermediaire  160   8.0   9.7  37.0    44.8
#> Cadre                     260  13.0  15.7  50.0    60.6
#> Employe                   594  29.7  35.9  79.8    96.5
#> Autre                      58   2.9   3.5  82.7   100.0
#> NA                        347  17.3    NA 100.0      NA
#> Total                    2000 100.0 100.0 100.0   100.0
freq(hdv2003$qualif, cum = TRUE, total = TRUE, sort ="dec")
#>                             n     %  val%  %cum val%cum
#> Employe                   594  29.7  35.9  29.7    35.9
#> Ouvrier qualifie          292  14.6  17.7  44.3    53.6
#> Cadre                     260  13.0  15.7  57.3    69.3
#> Ouvrier specialise        203  10.2  12.3  67.4    81.6
#> Profession intermediaire  160   8.0   9.7  75.4    91.3
#> Technicien                 86   4.3   5.2  79.8    96.5
#> Autre                      58   2.9   3.5  82.7   100.0
#> NA                        347  17.3    NA 100.0      NA
#> Total                    2000 100.0 100.0 100.0   100.0

# labelled data
data(fecondite)
freq(femmes$region)
#>             n    % val%
#> [1] Nord  707 35.4 35.4
#> [2] Est   324 16.2 16.2
#> [3] Sud   407 20.3 20.3
#> [4] Ouest 562 28.1 28.1
freq(femmes$region, levels = "l")
#>         n    % val%
#> Nord  707 35.4 35.4
#> Est   324 16.2 16.2
#> Sud   407 20.3 20.3
#> Ouest 562 28.1 28.1
freq(femmes$region, levels = "v")
#>     n    % val%
#> 1 707 35.4 35.4
#> 2 324 16.2 16.2
#> 3 407 20.3 20.3
#> 4 562 28.1 28.1