Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flextable() #50

Open
ASKurz opened this issue Jan 2, 2024 · 1 comment
Open

flextable() #50

ASKurz opened this issue Jan 2, 2024 · 1 comment

Comments

@ASKurz
Copy link
Owner

ASKurz commented Jan 2, 2024

Consider updating the book with flextable()-based tables. For example, here's how you might add in Table 5.2 (currently missing from the ebook):

# packages
library(tidyverse)
library(janitor)
library(flextable)

# load the data
d <- read_csv("data.R/HairEyeColor.csv")

# make the table
d %>%
  uncount(weights = Count, .remove = F) %>% 
  # the next 4 lines are from the janitor package
  tabyl(var1 = Eye, var2 = Hair) %>% 
  adorn_percentages(denominator = "all") %>% 
  adorn_totals(where = c("row", "col")) %>% 
  adorn_rounding(digits = 2) %>% 
  # data.frame() %>%  # same with or without
  rename(`Eye color` = Eye) %>% 
  # the remaining lines are from the flextable package
  flextable() %>% 
  hline(i = 4) %>% 
  vline(j = 5, part = "body") %>% 
  add_header_row(
    values = c("", "Hair color", ""),
    colwidths = c(1, 4, 1)) %>% 
  hline(
    i = 1,
    j = c(1, 6),
    border = fp_border_default(width = 0), 
    part = "header") %>% 
  align(
    i = 1, 
    j = 2:5,
    align = "center",
    part = "header"
  ) %>% 
  width(width = 0.8)
Screenshot 2024-01-02 at 9 34 23 AM
@ASKurz
Copy link
Owner Author

ASKurz commented Jan 2, 2024

Note that flextable offers a proc_freq() function for making contingency tables, which could in principle replace the functions from the janitor package. However, proc_freq() doesn't currently seen to have a nice way to convert the output to proportions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant