-3

I am preparing a summary statistic for categorical variables in R to put in an academic paper. I am looking for output like this:

Create summary table of categorical variables of different lengths However, I could not find a function for categorical variables.

Here is my small example:

library(dplyr)
library(stargazer)

mtcars %>%
  mutate(mpg_cat = ifelse(mpg > mean(mpg), 1,0)) %>%
  mutate(mpg_cat= as.factor(mpg_cat)) %>%
  mutate(cyl_cat= as.factor(cyl)) %>%
  select(cyl_cat, mpg_cat ) %>%
  function() %>% ##???
  stargazer(summary=FALSE, rownames=FALSE,
            #note you have to specify type
            type = "html",
            #note that the argument is "out" not "file"
            out="temp.doc")

and here is the output I have in my mind: https://i.stack.imgur.com/CIdIa.jpg

Hamideh
  • 665
  • 2
  • 8
  • 20

2 Answers2

1

Supposing that you have the data to fill a template like that use the library kableExtra:

https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf.

(See pages 14-20)

It's better for you to learn how to handle tables, also this library use %>% symbol which you alredy know.

0

I came up with this piece of code:

mtcars %>%
  mutate(mpg_cat = ifelse(mpg > mean(mpg), "Yes","No")) %>%
  mutate(mpg_cat= as.factor(mpg_cat)) %>%
  mutate(cyl_cat= as.factor(cyl)) %>%
  select(cyl_cat, mpg_cat ) %>%

  summary() %>%
  as.data.frame() %>%
  select(-Var1) %>%
  rename(Variable=Var2) %>%
  filter(! is.na(Freq) ) %>%
  separate(Freq, c("Level", "Freq."),sep=":" ) %>%
  mutate(Freq. = as.integer(Freq.)) %>%
  mutate(Total = nrow(mtcars)) %>%
  mutate(Perc. = Freq.*100/Total)  %>%
  select (-Total)  %>%

  stargazer(summary=FALSE, rownames=FALSE,
            #note you have to specify type
            type = "html",
            #note that the argument is "out" not "file"
            out="mtcars.doc")
Hamideh
  • 665
  • 2
  • 8
  • 20