Home > Software design >  R: Iterate fisher’s test over multiple rows in large dataframe to get output row-by-row
R: Iterate fisher’s test over multiple rows in large dataframe to get output row-by-row

Time:03-08

I have a large dataset with multiple categorical values that have different integer values (counts) in two different groups.

As an example

Element <- c("zinc", "calcium", "magnesium", "sodium", "carbon", "nitrogen")
no_A <- c(45, 143, 10, 35, 70, 40)
no_B <- c(10, 11, 1, 4, 40, 30)
elements_df <- data.frame(Element, no_A, no_B)
Element no_A no_B
Zinc 45 10
Calcium 143 11
Magnesium 10 1
Sodium 35 4
Carbon 70 40
Nitrogen 40 30

Previously I’ve just been using the code below and changing x manually to get the output values:

x = "calcium"

n1 = (elements_df %>% filter(Element== x))$no_A
n2 = sum(elements_df$no_A) - n1
n3 = (elements_df %>% filter(Element== x))$no_B
n4 = sum(elements_df$no_B) - n3

fisher.test(matrix(c(n1, n2, n3, n4), nrow = 2, ncol = 2, byrow = TRUE)) 

But I have a very large dataset with 4000 rows and I’d like the most efficient way to iterate through all of them and see which have significant p values.

I imagined I’d need a for loop and function, although I’ve looked through a few previous similar questions (none that I felt I could use) and it seems using apply might be the way to go.

So, in short, can anyone help me with writing code that iterates over x in each row and prints out the corresponding p values and odds ratio for each element?

CodePudding user response:

You could get them all in a nice data frame like this:

`row.names<-`(do.call(rbind, lapply(seq(nrow(elements_df)), function(i) {
f <- fisher.test(matrix(c(elements_df$no_A[i], sum(elements_df$no_A[-i]),
                     elements_df$no_B[i], sum(elements_df$no_B[-i])), nrow = 2));
data.frame(Element = elements_df$Element[i],
           "odds ratio" = f$estimate, "p value" = scales::pvalue(f$p.value),
           "Lower CI" = f$conf.int[1], "Upper CI" = f$conf.int[2],
           check.names = FALSE)
})), NULL)

#>     Element odds ratio p value  Lower CI    Upper CI
#> 1      zinc  1.2978966   0.601 0.6122734   3.0112485
#> 2   calcium  5.5065701  <0.001 2.7976646  11.8679909
#> 3 magnesium  2.8479528   0.469 0.3961312 125.0342574
#> 4    sodium  2.6090482   0.070 0.8983185  10.3719176
#> 5    carbon  0.3599468  <0.001 0.2158107   0.6016808
#> 6  nitrogen  0.2914476  <0.001 0.1634988   0.5218564
  • Related