Search code examples
rdataframefrequency

Using a loop to apply tab1 function from epidisplay


I have a dataset with 172 variables and I'm trying to generate frequency tables for all of the variables(they're all categorical/character variables). I'm using the tab1 function from the epiDisplay package. I know how to do it for individual variables like I've done in my code below, but is there a way to do it all at once? How would I do this using a loop or lapply?

library(epiDisplay)
tab1(data$Race1, sort.group = "decreasing", cum.percent = TRUE)

Thanks!


Solution

  • There might be several ways to do it. This may want to try something like this:

    library(epiDisplay)
    data <- mtcars # as an example data
    f_1 <- function(x){tab1(x, sort.group = "decreasing", cum.percent = TRUE)}
    lapply(data, f_1)
    

    If you do not need graph:

    f_1 <- function(x){tab1(x, sort.group = "decreasing", cum.percent = TRUE, graph = FALSE)}
    lapply(data, f_1)