Calculate Fisher's exact test p-value in dataframe rows

自古美人都是妖i 提交于 2019-12-25 15:35:20

问题


I have a list of 1700 samples in a data frame where every row represents the number of colorful items that every assistant has counted in a random number of specimens from different boxes. There are two available colors and two individuals counting the items so this could easily create a 2x2 contingency table.

df
Box-ID  1_Red  1_Blue  2_Red  2_Blue
1       1075   918     29     26
2       903    1076    135    144

I would like to know how can I treat every row as a contigency table (either vector or matrix) in order to perform a chi-square test (like Fisher's or Barnard's) and generate a sixth column with p-values. This is what I've tried so far, but I am not sure if it's correct

df$p-value = chisq.test(t(matrix(c(df[,1:4]), nrow=2)))$p.value 

回答1:


I think you could do something like this

df$p_value <- apply(df,1,function(x) fisher.test(matrix(x[-1],nrow=2))$p.value)


来源:https://stackoverflow.com/questions/38216800/calculate-fishers-exact-test-p-value-in-dataframe-rows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!