Calculate Fisher's exact test p-value in dataf

2019-09-13 01:30发布

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 

标签: r chi-squared
1条回答
\"骚年 ilove
2楼-- · 2019-09-13 02:15

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)
查看更多
登录 后发表回答