0

The main exposure variable is aff. I want to get contingency tables for aff and all variables in the varlist. Then I want to do chi-square test using these contingency tables. My codes are following:

name=names(data)
varlist=name[11:40]
models=lapply(varlist, function(x) {
          chisq.test(table(substitute(data$i,list(i = as.name(x))),data$aff))
 })
lapply(models, summary)

But I got error

Error in unique.default(x, nmax = nmax) : 
  unique() applies only to vectors

How to fix this?

hpesoj626
  • 3,529
  • 1
  • 17
  • 25
  • Perhaps [this](https://stackoverflow.com/questions/16681770/r-error-in-unique-defaultx-unique-applies-only-to-vectors) would be useful? – cacti5 Apr 28 '18 at 00:12
  • 1
    Paste the output of `dput(data)` into your question. At the present state of your question, we cannot reproduce your problem. – hpesoj626 Apr 28 '18 at 00:15
  • Ok, sure, if you want to retain the intermediate tables. Does this mean my answer resolves your question? If so, please "accept" the answer. – r2evans Apr 29 '18 at 00:42

2 Answers2

2

I think you're over-complicating things by using substitute and such. Without your data, I'll try with mtcars, using cyl as the exposure variable.

data <- mtcars
name <- names(data)
ev <- "cyl"
varlist <- name[ name != ev ]
models <- lapply(varlist, function(nm) {
  chisq.test(table(data[[nm]], data[[ev]]))
})
# Warning messages:
# 1: In chisq.test(table(data[[nm]], data[[ev]])) :
#   Chi-squared approximation may be incorrect

(because I'm using a bad example for the test, there are a lot of warnings here; this can be ignored when using mtcars because it is really not a good dataset for this test.)

summaries <- lapply(models, summary)
str(summaries[1:2])
# List of 2
#  $ : 'summaryDefault' chr [1:9, 1:3] " 1" " 1" " 1" " 1" ...
#   ..- attr(*, "dimnames")=List of 2
#   .. ..$ : chr [1:9] "statistic" "parameter" "p.value" "method" ...
#   .. ..$ : chr [1:3] "Length" "Class" "Mode"
#  $ : 'summaryDefault' chr [1:9, 1:3] " 1" " 1" " 1" " 1" ...
#   ..- attr(*, "dimnames")=List of 2
#   .. ..$ : chr [1:9] "statistic" "parameter" "p.value" "method" ...
#   .. ..$ : chr [1:3] "Length" "Class" "Mode"
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Thanks for your help. It is very illuminating, but I revise ur codes a little bit. ------------------------------------- lapply(varlist, function(nm) { table(registry[[nm]], registry[[ev]]) }) lapply(models,chisq.test) – Tong Zhang Apr 28 '18 at 20:37
0

Supposing your data is like mtcars, where vs, am, gear, and carb are categorical variables, you can create a function like so:

df_list_f <- function(x) chisq.test(table(df2$cyl, x))
df2 <- mtcars[,8:11] # df2 contains the columns vs, am, gear and carb
lapply(df2, df_list_f)
hpesoj626
  • 3,529
  • 1
  • 17
  • 25
  • Thanks for your help. But I can't follow your code. What's x? df2$cyl? – Tong Zhang Apr 28 '18 at 20:42
  • `df2` is your `data`. `cyl` is your `aff`. I am just demonstrating a procedure that you can replicate. Provide your data and we can taylor this solution to it. – hpesoj626 Apr 28 '18 at 23:11