Search code examples
rloopslapplycrosstabcrosstable

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.


Solution

  • 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