This function returns a frequency table of labelled vectors, as data frame.
frq(x, ..., sort.frq = c("none", "asc", "desc"), weights = NULL, auto.grp = NULL, show.strings = TRUE, grp.strings = NULL, out = c("txt", "viewer", "browser"), title = NULL, weight.by)
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 |
sort.frq | Determines whether categories should be sorted
according to their frequencies or not. Default is |
weights | Bare name, or name as string, of a variable in |
auto.grp | Numeric value, indicating the minimum amount of unique
values in a variable, at which automatic grouping into smaller units
is done (see |
show.strings | Logical, if |
grp.strings | Numeric, if not |
out | Character vector, indicating whether the results should be printed
to console ( |
title | String, will be used as alternative title to the variable
label. If |
weight.by | Deprecated. |
A list of data frames with values, value labels, frequencies, raw, valid and
cumulative percentages of x
.
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.
flat_table
for labelled (proportional) tables.
# simple vector data(efc) frq(efc$e42dep)#> #> # elder's dependency (x) <numeric> #> # total N=908 valid N=901 mean=2.94 sd=0.94 #> #> val label frq raw.prc valid.prc cum.prc #> 1 independent 66 7.27 7.33 7.33 #> 2 slightly dependent 225 24.78 24.97 32.30 #> 3 moderately dependent 306 33.70 33.96 66.26 #> 4 severely dependent 304 33.48 33.74 100.00 #> NA NA 7 0.77 NA NA #> #># with grouped data frames, in a pipe library(dplyr) efc %>% group_by(e16sex, c172code) %>% frq(e16sex, c172code, e42dep)#> #> Grouped by: #> elder's gender: male #> carer's level of education: low level of education #> #> # elder's dependency (e42dep) <numeric> #> # total N=80 valid N=80 mean=3.06 sd=0.92 #> #> val label frq raw.prc valid.prc cum.prc #> 1 independent 5 6.25 6.25 6.25 #> 2 slightly dependent 16 20.00 20.00 26.25 #> 3 moderately dependent 28 35.00 35.00 61.25 #> 4 severely dependent 31 38.75 38.75 100.00 #> NA NA 0 0.00 NA NA #> #> #> Grouped by: #> elder's gender: male #> carer's level of education: intermediate level of education #> #> # elder's dependency (e42dep) <numeric> #> # total N=156 valid N=156 mean=2.83 sd=0.94 #> #> val label frq raw.prc valid.prc cum.prc #> 1 independent 15 9.62 9.62 9.62 #> 2 slightly dependent 39 25.00 25.00 34.62 #> 3 moderately dependent 59 37.82 37.82 72.44 #> 4 severely dependent 43 27.56 27.56 100.00 #> NA NA 0 0.00 NA NA #> #> #> Grouped by: #> elder's gender: male #> carer's level of education: high level of education #> #> # elder's dependency (e42dep) <numeric> #> # total N=43 valid N=43 mean=2.91 sd=0.81 #> #> val label frq raw.prc valid.prc cum.prc #> 1 independent 1 2.33 2.33 2.33 #> 2 slightly dependent 13 30.23 30.23 32.56 #> 3 moderately dependent 18 41.86 41.86 74.42 #> 4 severely dependent 11 25.58 25.58 100.00 #> NA NA 0 0.00 NA NA #> #> #> Grouped by: #> elder's gender: female #> carer's level of education: low level of education #> #> # elder's dependency (e42dep) <numeric> #> # total N=99 valid N=99 mean=2.95 sd=0.94 #> #> val label frq raw.prc valid.prc cum.prc #> 1 independent 7 7.07 7.07 7.07 #> 2 slightly dependent 25 25.25 25.25 32.32 #> 3 moderately dependent 33 33.33 33.33 65.66 #> 4 severely dependent 34 34.34 34.34 100.00 #> NA NA 0 0.00 NA NA #> #> #> Grouped by: #> elder's gender: female #> carer's level of education: intermediate level of education #> #> # elder's dependency (e42dep) <numeric> #> # total N=350 valid N=350 mean=2.90 sd=0.98 #> #> val label frq raw.prc valid.prc cum.prc #> 1 independent 30 8.57 8.57 8.57 #> 2 slightly dependent 96 27.43 27.43 36.00 #> 3 moderately dependent 104 29.71 29.71 65.71 #> 4 severely dependent 120 34.29 34.29 100.00 #> NA NA 0 0.00 NA NA #> #> #> Grouped by: #> elder's gender: female #> carer's level of education: high level of education #> #> # elder's dependency (e42dep) <numeric> #> # total N=113 valid N=113 mean=3.04 sd=0.85 #> #> val label frq raw.prc valid.prc cum.prc #> 1 independent 4 3.54 3.54 3.54 #> 2 slightly dependent 26 23.01 23.01 26.55 #> 3 moderately dependent 44 38.94 38.94 65.49 #> 4 severely dependent 39 34.51 34.51 100.00 #> NA NA 0 0.00 NA NA #> #># with select-helpers: all variables from the COPE-Index # (which all have a "cop" in their name) frq(efc, contains("cop"))#> #> # do you feel you cope well as caregiver? (c82cop1) <numeric> #> # total N=908 valid N=901 mean=3.12 sd=0.58 #> #> val label frq raw.prc valid.prc cum.prc #> 1 never 3 0.33 0.33 0.33 #> 2 sometimes 97 10.68 10.77 11.10 #> 3 often 591 65.09 65.59 76.69 #> 4 always 210 23.13 23.31 100.00 #> NA NA 7 0.77 NA NA #> #> #> # do you find caregiving too demanding? (c83cop2) <numeric> #> # total N=908 valid N=902 mean=2.02 sd=0.72 #> #> val label frq raw.prc valid.prc cum.prc #> 1 Never 186 20.48 20.62 20.62 #> 2 Sometimes 547 60.24 60.64 81.26 #> 3 Often 130 14.32 14.41 95.68 #> 4 Always 39 4.30 4.32 100.00 #> NA NA 6 0.66 NA NA #> #> #> # does caregiving cause difficulties in your relationship with your friends? (c84cop3) <numeric> #> # total N=908 valid N=902 mean=1.63 sd=0.87 #> #> val label frq raw.prc valid.prc cum.prc #> 1 Never 516 56.83 57.21 57.21 #> 2 Sometimes 252 27.75 27.94 85.14 #> 3 Often 82 9.03 9.09 94.24 #> 4 Always 52 5.73 5.76 100.00 #> NA NA 6 0.66 NA NA #> #> #> # does caregiving have negative effect on your physical health? (c85cop4) <numeric> #> # total N=908 valid N=898 mean=1.77 sd=0.87 #> #> val label frq raw.prc valid.prc cum.prc #> 1 Never 409 45.04 45.55 45.55 #> 2 Sometimes 346 38.11 38.53 84.08 #> 3 Often 85 9.36 9.47 93.54 #> 4 Always 58 6.39 6.46 100.00 #> NA NA 10 1.10 NA NA #> #> #> # does caregiving cause difficulties in your relationship with your family? (c86cop5) <numeric> #> # total N=908 valid N=902 mean=1.39 sd=0.67 #> #> val label frq raw.prc valid.prc cum.prc #> 1 Never 626 68.94 69.40 69.40 #> 2 Sometimes 211 23.24 23.39 92.79 #> 3 Often 50 5.51 5.54 98.34 #> 4 Always 15 1.65 1.66 100.00 #> NA NA 6 0.66 NA NA #> #> #> # does caregiving cause financial difficulties? (c87cop6) <numeric> #> # total N=908 valid N=900 mean=1.29 sd=0.64 #> #> val label frq raw.prc valid.prc cum.prc #> 1 Never 713 78.52 79.22 79.22 #> 2 Sometimes 131 14.43 14.56 93.78 #> 3 Often 39 4.30 4.33 98.11 #> 4 Always 17 1.87 1.89 100.00 #> NA NA 8 0.88 NA NA #> #> #> # do you feel trapped in your role as caregiver? (c88cop7) <numeric> #> # total N=908 valid N=900 mean=1.92 sd=0.91 #> #> val label frq raw.prc valid.prc cum.prc #> 1 Never 336 37.00 37.33 37.33 #> 2 Sometimes 374 41.19 41.56 78.89 #> 3 Often 113 12.44 12.56 91.44 #> 4 Always 77 8.48 8.56 100.00 #> NA NA 8 0.88 NA NA #> #> #> # do you feel supported by friends/neighbours? (c89cop8) <numeric> #> # total N=908 valid N=901 mean=2.16 sd=1.04 #> #> val label frq raw.prc valid.prc cum.prc #> 1 never 313 34.47 34.74 34.74 #> 2 sometimes 237 26.10 26.30 61.04 #> 3 often 241 26.54 26.75 87.79 #> 4 always 110 12.11 12.21 100.00 #> NA NA 7 0.77 NA NA #> #> #> # do you feel caregiving worthwhile? (c90cop9) <numeric> #> # total N=908 valid N=888 mean=2.93 sd=0.96 #> #> val label frq raw.prc valid.prc cum.prc #> 1 never 76 8.37 8.56 8.56 #> 2 sometimes 210 23.13 23.65 32.21 #> 3 often 300 33.04 33.78 65.99 #> 4 always 302 33.26 34.01 100.00 #> NA NA 20 2.20 NA NA #> #># all variables from column "c161sex" to column "c175empl" frq(efc, c161sex:c175empl)#> #> # carer's gender (c161sex) <numeric> #> # total N=908 valid N=901 mean=1.76 sd=0.43 #> #> val label frq raw.prc valid.prc cum.prc #> 1 Male 215 23.68 23.86 23.86 #> 2 Female 686 75.55 76.14 100.00 #> NA NA 7 0.77 NA NA #> #> #> # carer's level of education (c172code) <numeric> #> # total N=908 valid N=842 mean=1.97 sd=0.63 #> #> val label frq raw.prc valid.prc cum.prc #> 1 low level of education 180 19.82 21.38 21.38 #> 2 intermediate level of education 506 55.73 60.10 81.47 #> 3 high level of education 156 17.18 18.53 100.00 #> NA NA 66 7.27 NA NA #> #> #> # are you currently employed? (c175empl) <numeric> #> # total N=908 valid N=902 mean=0.43 sd=0.49 #> #> val label frq raw.prc valid.prc cum.prc #> 0 no 518 57.05 57.43 57.43 #> 1 yes 384 42.29 42.57 100.00 #> NA NA 6 0.66 NA NA #> #># for non-labelled data, variable name is printed, # and "label" column is removed from output data(iris) frq(iris, Species)#> #> # Species <categorical> #> # total N=150 valid N=150 mean=2.00 sd=0.82 #> #> val frq raw.prc valid.prc cum.prc #> setosa 50 33.33 33.33 33.33 #> versicolor 50 33.33 33.33 66.67 #> virginica 50 33.33 33.33 100.00 #> <NA> 0 0.00 NA NA #> #># group variables with large range and with weights efc$weights <- abs(rnorm(n = nrow(efc), mean = 1, sd = .5)) frq(efc, c160age, auto.grp = 5, weights = weights)#> #> # carer' age (c160age) <numeric> #> # total N=920 valid N=920 mean=53.85 sd=13.09 #> #> val label frq raw.prc valid.prc cum.prc #> 1 18-32 56 6.09 6.09 6.09 #> 2 33-47 234 25.43 25.43 31.52 #> 3 48-62 380 41.30 41.30 72.83 #> 4 63-77 223 24.24 24.24 97.07 #> 5 78-92 27 2.93 2.93 100.00 #> NA NA 0 0.00 NA NA #> #># group string values dummy <- efc[1:50, 3, drop = FALSE] dummy$words <- sample( c("Hello", "Helo", "Hole", "Apple", "Ape", "New", "Old", "System", "Systemic"), size = nrow(dummy), replace = TRUE ) frq(dummy)#> #> # e16sex <numeric> #> # total N=50 valid N=50 mean=1.56 sd=0.50 #> #> val frq raw.prc valid.prc cum.prc #> 1 22 44 44 44 #> 2 28 56 56 100 #> <NA> 0 0 NA NA #> #> #> # words <character> #> # total N=50 valid N=50 mean=5.54 sd=2.63 #> #> val frq raw.prc valid.prc cum.prc #> Ape 3 6 6 6 #> Apple 4 8 8 14 #> Hello 7 14 14 28 #> Helo 8 16 16 44 #> Hole 2 4 4 48 #> New 4 8 8 56 #> Old 5 10 10 66 #> System 9 18 18 84 #> Systemic 8 16 16 100 #> <NA> 0 0 NA NA #> #>frq(dummy, grp.strings = 2)#> #> # e16sex <numeric> #> # total N=50 valid N=50 mean=1.56 sd=0.50 #> #> val frq raw.prc valid.prc cum.prc #> 1 22 44 44 44 #> 2 28 56 56 100 #> <NA> 0 0 NA NA #> #> #> # words <categorical> #> # total N=50 valid N=50 mean=3.16 sd=1.54 #> #> val frq raw.prc valid.prc cum.prc #> Ape, Apple 7 14 14 14 #> Hello, Helo, Hole 17 34 34 48 #> New 4 8 8 56 #> Old 5 10 10 66 #> System, Systemic 17 34 34 100 #> <NA> 0 0 NA NA #> #>