frq: Frequency table of labelled variables

Description Usage Arguments Value Note See Also Examples

View source: R/frq.R

Description

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

Usage

1
2
3
frq(x, ..., sort.frq = c("none", "asc", "desc"), weight.by = NULL,
  auto.grp = NULL, show.strings = TRUE, grp.strings = NULL,
  out = c("txt", "viewer", "browser"))

Arguments

x

A vector or a data frame. May also be a grouped data frame (see 'Note' and 'Examples').

...

Optional, unquoted names of variables that should be selected for further processing. Required, if x is a data frame (and no vector) and only selected variables from x should be processed. You may also use functions like : or tidyselect's select_helpers. See 'Examples' or package-vignette.

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

Bare name, or name as string, of a variable in x that indicates the vector of weights, which will be applied to weight all observations. Default is NULL, so no weights are used.

auto.grp

Numeric value, indicating the minimum amount of unique values in a variable, at which automatic grouping into smaller units is done (see group_var). Default value for auto.group is NULL, i.e. auto-grouping is off.

show.strings

Logical, if TRUE, frequency tables for character vectors will not be printed. This is useful when printing frequency tables of all variables from a data frame, and due to computational reasons character vectors should not be printed.

grp.strings

Numeric, if not NULL, groups string values in character vectors, based on their similarity. The similarity is estimated with the stringdist-package. See group_str for details on grouping, and that function's maxdist-argument to get more details on the distance of strings to be treated as equal.

out

Character vector, indicating whether the results should be printed to console (out = "txt") or as HTML-table in the viewer-pane (out = "viewer") or browser (out = "browser").

Value

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

Note

x may also be a grouped data frame (see group_by) with up to two grouping variables. Frequency tables are created for each subgroup then.

The print()-method adds a table header with information on the variable label, variable type, total and valid N, and mean and standard deviations. Mean and SD are always printed, even for categorical variables (factors) or character vectors. In this case, values are coerced into numeric vector to calculate the summary statistics.

See Also

flat_table for labelled (proportional) tables.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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()

# or:
# frq(efc, e42dep, e15relat, c172code)

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

# with select-helpers: all variables from the COPE-Index
# (which all have a "cop" in their name)
frq(efc, contains("cop"))

# all variables from column "c161sex" to column "c175empl"
frq(efc, c161sex:c175empl)

# for non-labelled data, variable name is printed,
# and "label" column is removed from output
data(iris)
frq(iris, Species)

# group variables with large range
frq(efc, c160age)
frq(efc, c160age, auto.grp = 5)

# and with weights
efc$weights <- abs(rnorm(n = nrow(efc), mean = 1, sd = .5))
frq(efc, c160age, auto.grp = 5, weight.by = weights)

# group string values
## Not run: 
dummy <- efc %>% dplyr::select(3)
dummy$words <- sample(
  c("Hello", "Helo", "Hole", "Apple", "Ape",
    "New", "Old", "System", "Systemic"),
  size = nrow(dummy),
  replace = TRUE
)

frq(dummy)
frq(dummy, grp.strings = 2)
## End(Not run)

strengejacke/sjmisc documentation built on Aug. 14, 2018, noon