data(efc)
# proportion of value 1 in e42dep
prop(efc, e42dep == 1)
# expression may also be completely quoted
prop(efc, "e42dep == 1")
# proportion of value 1 in e42dep, and all values greater
# than 2 in e42dep, excluding missing values. will return a tibble
prop(efc, e42dep == 1, e42dep > 2, na.rm = TRUE)
# for factors or character vectors, use quoted or unquoted values
library(sjmisc)
# convert numeric to factor, using labels as factor levels
efc$e16sex <- to_label(efc$e16sex)
# get proportion of female older persons
prop(efc, e16sex == female)
# get proportion of male older persons
prop(efc, e16sex == "male")
# also works with pipe-chains
library(dplyr)
efc %>% prop(e17age > 70)
efc %>% prop(e17age > 70, e16sex == 1)
# and with group_by
efc %>%
group_by(e16sex) %>%
prop(e42dep > 2)
efc %>%
select(e42dep, c161sex, c172code, e16sex) %>%
group_by(c161sex, c172code) %>%
prop(e42dep > 2, e16sex == 1)
Run the code above in your browser using DataLab