Shannon
Shannon

Reputation: 93

Using loops to create multiple crosstables in R

I have a dataset with one main categorical outcome variable and multiple categorical exposure variables. I'd like to generate a series of individual crosstabs with Chi Square tests for each exposure variable, and I'm wondering if there's a way to do that using a loop.

This is essentially the long version of what I'm trying to accomplish:

    ctable(data$x1, data$y, chisq=T, useNA='no')
    ctable(data$x2, data$y, chisq=T, useNA='no')
    ctable(data$x3, data$y, chisq=T, useNA='no')

This was my first pass at turning that into a loop:

    for(i in c('x1', 'x2', 'x3')){
    ctables <- ctable(data[[i]], data$y, chisq=T, useNA='no')
    }
    ctables

I don't get any errors when I run that, but it only returns a ctable for the last variable name (in this example, x3). What am I missing?

Note: I need to specifically name the data frame (data$y as opposed to just saying y) because I'm working with a large SQL database that includes multiple data frames.

Upvotes: 0

Views: 483

Answers (2)

Dan Chaltiel
Dan Chaltiel

Reputation: 8523

Alternatively, you could use the package {crosstable} which allows you to ask for several variables as input.

Here is an example using the mtcars dataset:

library(crosstable)
crosstable(mtcars2, c(am, gear, cyl), by=vs, total=TRUE, 
           test=TRUE, showNA='no', percent_digits=1) %>% 
  as_flextable()

crosstable

Upvotes: 1

langtang
langtang

Reputation: 24867

You can use lapply() instead:

ctables <- lapply(c('x1', 'x2', 'x3'), function(i) {
  ctable(data[[i]], data$y, chisq=T, useNA='no', dnn =c(i,"y"))
})

** Updated with dnn=c(i,"y") to specify explicit names in resulting ctable

Upvotes: 0

Related Questions