1

I have a data frame df in R with categorical variables: column names of the variables are 'A', 'B', 'C' and another categorical variable is 'gene'

I want to run pairwise fisher exact test in R. When I run it one by one like this:

A <-  xtabs(~ A + gene, data = df)
pairwise_fisher_test(A ,p.adjust.method = 'fdr')

B <-  xtabs(~ B + gene, data = df)
pairwise_fisher_test(B ,p.adjust.method = 'fdr')

C <-  xtabs(~ C + gene, data = df)
pairwise_fisher_test(C ,p.adjust.method = 'fdr')

this works!!!

Now I am trying to use lapply like this:

vec <- ('A','B','C')
lapply(vec,function(x){
  xtabs(~ x + gene, data = df)
  pairwise_fisher_test(xtabs(~ x + gene, data = df) , p.adjust.method = 'fdr')
})

I get this error:

Error in model.frame.default(formula = ~x + gene, data = df): variable lengths differ (found for 'gene')

What am I doing wrong??

user3138373
  • 519
  • 1
  • 6
  • 18
  • You cannot drop character values into formulas that way. Learn to use `reformulate`. Many answered Qs on SO should be available. – IRTFM May 16 '23 at 21:35

1 Answers1

1

I found the solution. Actually in lapply, I was passing the formula in a wrong way. Here is the correct way:

vec <- c('A', 'B', 'C')

lapply(vec, function(x) {
  formula <- as.formula(paste("~", x, "+ gene"))
  table <- xtabs(formula, data = df)
  pairwise_fisher_test(table, p.adjust.method = 'fdr')
})

Here, we use as.formula() to create a formula object by combining the variable x with the string "gene". This way, the formula is correctly constructed and passed to xtabs() and pairwise_fisher_test().

user3138373
  • 519
  • 1
  • 6
  • 18
  • There must be twenty similar answered questions on SO. – IRTFM May 16 '23 at 21:42
  • @IRTFM Feel free to link a duplicate. (Instead of `as.formula(....)` you can also use `formula <- reformulate(c(x, 'gene'))`) – Axeman May 16 '23 at 21:43