Learn R Programming

sjmisc (version 2.2.0)

frq: Frequencies of labelled variables

Description

This function returns a frequency table of labelled vectors, as data frame.

Usage

frq(x, sort.frq = c("none", "asc", "desc"), weight.by = NULL)

Arguments

x
A labelled vector or a data.frame with labelled vectors. May also be a grouped data frame (see 'Note' and 'Examples').
sort.frq
Determines whether categories should be sorted according to their frequencies or not. Default is "none", so categories are not sorted by frequency. Use "asc" or "desc" for sorting categories ascending or descending order.
weight.by
Vector of weights that will be applied to weight all observations. Must be a vector of same length as the input vector. Default is NULL, so no weights are used.

Value

A data frame with values, value labels, frequencies, raw, valid and cumulative percentages of x.

See Also

flat_table for labelled (proportional) tables.

Examples

Run this code
library(haven)
# create labelled integer
x <- labelled(c(1, 2, 1, 3, 4, 1),
              c(Male = 1, Female = 2, Refused = 3, "N/A" = 4))
frq(x)

x <- labelled(c(1:3, tagged_na("a", "c", "z"), 4:1, 2:3),
              c("Agreement" = 1, "Disagreement" = 4, "First" = tagged_na("c"),
                "Refused" = tagged_na("a"), "Not home" = tagged_na("z")))
frq(x)

# in a pipe
data(efc)
library(dplyr)
efc %>% select(e42dep, e15relat, c172code) %>% frq()

# with grouped data frames, in a pipe
efc %>%
  group_by(e16sex, c172code) %>%
  select(e16sex, c172code, e42dep) %>%
  frq()

Run the code above in your browser using DataLab