RobertF
RobertF

Reputation: 904

How to exclude cumulative and total proportions from tables generated with `freq` function in R?

I'm using the freq function from the summarytools package to create frequency tables in RStudio.

It doesn't seem possible to turn off the cumulative and total percentage columns in the tables. For example:

library(summarytools)
data(mtcars)
view(freq(mtcars$cyl, totals=FALSE, cumul=FALSE))

still produces a table containing duplicate cumulative and total percentage columns. All I need is a table with the variable values, count #, and percentage.

I've tried resetting the global options with st_options(freq.cumul = FALSE, freq.totals = FALSE) but receive an error message:

Error in st_options(freq.cumul = FALSE, freq.totals = FALSE) : 
  unused arguments (freq.cumul = FALSE, freq.totals = FALSE)

UPDATE

Finally figured it out - I wasn't using enough arguments in the freq function. The following code produces a decent frequency table:

cyl_freq <- freq(mtcars$cyl, report.nas = FALSE, totals=FALSE, cumul=FALSE, style = "rmarkdown", headings = FALSE);
view(cyl_freq)

and if you need to create a bunch of tables across multiple columnsmultiple_:

multiple_freq <- lapply(mtcars[c(2,8:11)], function(x) freq(x, report.nas = FALSE, totals=FALSE, cumul=FALSE, headings = FALSE));
view(multiple_freq)

Upvotes: 3

Views: 534

Answers (2)

Dominic Comtois
Dominic Comtois

Reputation: 10431

Seems you found how to make it work... Just as a tip, you can skip the lapply part. So this should work as expected:

library(summarytools)
freq(mtcars[c(2,8:11)], 
     report.nas=FALSE, totals=FALSE, cumul=FALSE, headings=FALSE)

There was an issue where the cumul argument didn't register when doing this in versions prior to 0.9.8, but it's fixed. Version 0.9.8 will be on CRAN any day now, but you can always install the latest version from GitHub with remotes::install_github("dcomtois/summarytools")

Upvotes: 1

Matt
Matt

Reputation: 2987

This isn't using the summarytools package, but I think this may be what you're looking for.

frtable <- table(mtcars$cyl)

percent <- prop.table(frtable)

dt <- cbind(frtable , percent) %>% set_colnames(c("Count", "Percent"))

DT::datatable(dt) %>% DT::formatPercentage('percent')

Upvotes: 1

Related Questions