Search code examples
rfrequencygt

How to order frequency from highest to lowest for character variable


Suppose my dataframe (df) only includes this single character variable:

race.ethnicity<-c("W", "C", "F", "F", "J")

I want to create a frequency table for the top 2 categories. Like the table below (although it includes the top 15 categories)

enter image description here

I am using gtsummary for my frequency table.

Here are the codes:

# summarize the subdata
table1 <- tbl_summary(df, missing = "always",                              
                      missing_text = "(Missing)",
                      percent = "cell", 
                      type = all_dichotomous() ~"categorical"
) %>%
  bold_labels()
#export to latex(pdf is not available in the package)
as_kable_extra(table1, format = "latex")

With my current set of codes, I don't get the output by frequency. So any suggestions would be welcome.

If there are other suggestions to create a table like the one above besides using gtsummary then please share as well. I just want R to spit out the Latex codes as well.


Solution

  • Use xtabs to make a frequency count, convert that to data frame, sort and take the first two rows. No packages are used.

    dat <- as.data.frame(xtabs(~ race.ethnicity))
    dat2 <- head(dat[order(-dat$Freq), ], 2)
    dat2
    

    giving:

      race.ethnicity Freq
    2              F    2
    1              C    1
    

    To get latex:

    library(kableExtra)
    kable(dat2, "latex")
    

    giving:

    \begin{tabular}{l|l|r}
    \hline
      & race.ethnicity & Freq\\
    \hline
    2 & F & 2\\
    \hline
    1 & C & 1\\
    \hline
    \end{tabular}
    

    or write it as the following pipeline:

     library(dplyr)
     library(kableExtra)
    
     xtabs(~ race.ethnicity) %>%
       as.data.frame %>%
       arrange(desc(Freq)) %>%
       slice(1:2) %>%
       kable("latex")
    

    or

     library(kableExtra)
    
     xtabs(~ race.ethnicity) %>%
       { .[order(- .)] } %>%
       head(2) %>%
       kable("latex")