if (FALSE) { # getRversion() >= "4.2.0" && requireNamespace("datawizard", quietly = TRUE)
data(efc)
# proportion of value 1 in e42dep
prop(efc, e42dep == 1)
# expression may also be completely quoted
prop(efc, "e42dep == 1")
# use "props()" for multiple logical statements
props(efc, e17age > 70 & e17age < 80)
# proportion of value 1 in e42dep, and all values greater
# than 2 in e42dep, including missing values. will return a data frame
prop(efc, e42dep == 1, e42dep > 2, na.rm = FALSE)
# for factors or character vectors, use quoted or unquoted values
library(datawizard)
# convert numeric to factor, using labels as factor levels
efc$e16sex <- to_factor(efc$e16sex)
efc$n4pstu <- to_factor(efc$n4pstu)
# get proportion of female older persons
prop(efc, e16sex == female)
# get proportion of male older persons
prop(efc, e16sex == "male")
# "props()" needs quotes around non-numeric factor levels
props(efc,
e17age > 70 & e17age < 80,
n4pstu == 'Care Level 1' | n4pstu == 'Care Level 3'
)
# also works with pipe-chains
efc |> prop(e17age > 70)
efc |> prop(e17age > 70, e16sex == 1)
}
Run the code above in your browser using DataLab