brad-cannell / r4epi

Repository for the R for Epidemiology book
http://www.r4epi.com/
Other
18 stars 50 forks source link

Add a section on using freq_format #145

Open mbcann01 opened 3 months ago

mbcann01 commented 3 months ago

Using freq_format

Notice that once again our percentages are being displayed with 5 digits to the right of the decimal. Like I said above, we would almost never want to display this many digits. Additionally, we would typically want to reduce (e.g., we don't need the standard error or critical value from the t distribution) and format (e.g., add parenthesess) to our results before moving them to a publication or report. The freq_format() function is another function I wrote, which is intended to make it quick and easy to format the output of the freq_table() function for tables that may be used in publications.

Let's say that we want to present the results of the analysis above a the count (n) and percent of students that fall into each category. Additionally, let's say that we wanted to round all values to whole integers. We could do so like this:


height_and_weight_20 %>% 

  filter(!is.na(sex)) %>% 

  freq_table(sex) %>% 

  freq_format(

    recipe = "n (percent%)",

    name = "n_percent",

    digits = 2

  ) %>% 

  select(var, cat, n_percent)