0

I have data dataframe with two groups, Tumor and Normal. for each site/row i want calculate fischer exact for using Methyl UnMethy between Tumor and Normal

I'm looking for how to transform data to calculate fisher exact for each site using dplyr approach.

methyl_dat <- data.frame(loci = c("site1", "site2", "site3", "site4"), 
           Methy.tumor = c(50, 5, 60, 12), 
           UnMethy.tumor = c(60, 0, 65, 5), 
           Methy.Normal = c(13, 5, 22, 3),
           UnMethy.Normal = c(86, 0, 35, 3) )

Here is Fischer exact strategy for site 1

              Normal
Tumor          Methyl  UnMethy
  Methy         50      13
  UnMethy       60      86
sahuno
  • 321
  • 1
  • 8

2 Answers2

5

consider doing:

apply(methyl_dat[-1], 1, \(x)fisher.test(matrix(x,2)), simplify = F)
Onyambu
  • 67,392
  • 3
  • 24
  • 53
2

Adding a dplyr solution:

library(dplyr, warn.conflicts = FALSE)
data.frame(loci = c("site1", "site2", "site3", "site4"), 
           Methy.tumor = c(50, 5, 60, 12), 
           UnMethy.tumor = c(60, 0, 65, 5), 
           Methy.Normal = c(13, 5, 22, 3),
           UnMethy.Normal = c(86, 0, 35, 3) ) %>% 
  group_by(loci) %>% 
  summarise(
    p_val = fisher.test(matrix(c_across(everything()), 2))$p.val
  )
#> # A tibble: 4 × 2
#>   loci        p_val
#>   <chr>       <dbl>
#> 1 site1 0.000000392
#> 2 site2 1          
#> 3 site3 0.263      
#> 4 site4 0.621

Created on 2023-06-01 with reprex v2.0.2

Baraliuh
  • 2,009
  • 5
  • 11