We will continue to work with the same data as in the previous set of exercises. By now, you should (hopefully) have saved them as an .rds
file and, hence, be able to easily load them (in case they are not already/still in your current R
workspace/working environment).
corona_survey <- readRDS("./data/corona_survey.rds")
base R
, print a simple table with the frequencies of the variable education_cat
. Also include the counts for missing values.
useNA = "always"
.
table(corona_survey$education_cat, useNA = "always")
##
## Low Medium High <NA>
## 423 1154 2188 0
base R
functions to get the proportions for the variable sex
rounded to four decimal places.
table()
into two other functions: One for creating the proportion table and another one for rounding the decimal places.
round(prop.table(table(corona_survey$sex)), 4)
##
## Male Female
## 0.5134 0.4866
dplyr
package to get the frequencies and proportions for the sex
variable (without worrying about the number of decimal places this time).
corona_survey %>%
count(sex) %>%
mutate(proportion = n/sum(n)) %>%
ungroup()
## # A tibble: 2 x 3
## sex n proportion
## <fct> <int> <dbl>
## 1 Male 1933 0.513
## 2 Female 1832 0.487
summarytools
package to display the counts and percentages for the categories in the age_cat
variable. The output should not include information about the totals.
freq(corona_survey$age_cat,
totals = FALSE)
## Frequencies
## corona_survey$age_cat
## Type: Ordered Factor
##
## Freq % Valid % Valid Cum. % Total % Total Cum.
## -------------------- ------ --------- -------------- --------- --------------
## <= 25 years 107 2.84 2.84 2.84 2.84
## 26 to 30 years 267 7.09 9.93 7.09 9.93
## 31 to 35 years 276 7.33 17.26 7.33 17.26
## 36 to 40 years 328 8.71 25.98 8.71 25.98
## 41 to 45 years 317 8.42 34.40 8.42 34.40
## 46 to 50 years 367 9.75 44.14 9.75 44.14
## 51 to 60 years 978 25.98 70.12 25.98 70.12
## 61 to 65 years 386 10.25 80.37 10.25 80.37
## 66 to 70 years 357 9.48 89.85 9.48 89.85
## >= 71 years 382 10.15 100.00 10.15 100.00
## <NA> 0 0.00 100.00