如何将R中的数据帧转换为列联表?

如何将R中的数据帧转换为列联表?,r,dataframe,contingency,R,Dataframe,Contingency,我有一个简单的问题。如何将数据帧转换为Fisher精确测试的列联表 我有数据约19000行: head(data) R_T1 R_T2 NR_T1 NR_T2 GMNN 14 60 70 157 GORASP2 7 67 39 188 TTC34 5 69 41 186 ZXDC 8 66 37 190 ASAH2

我有一个简单的问题。如何将数据帧转换为Fisher精确测试的列联表

我有
数据
约19000行:

head(data)

          R_T1   R_T2    NR_T1  NR_T2
GMNN      14      60     70     157
GORASP2    7      67     39     188
TTC34      5      69     41     186
ZXDC       8      66     37     190
ASAH2      9      65     46     181
我想将每一行转换为一个列联表,以执行Fisher精确测试。例如,对于
GMNN

       R   NR
T1    14   70
T2    60  157

fisher.test(GMNN, alternative="two.sided")

Fisher's Exact Test for Count Data

data:  GMNN
p-value = 0.05273
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
0.2531445 1.0280271
sample estimates:
odds ratio 
0.5243787 
由于我有19000行数据,我希望输出为:

          R_T1   R_T2    NR_T1  NR_T2    p-value    odds_ratio
GMNN      14      60     70     157      0.05273    0.5243787 
GORASP2    7      67     39     188       0.1367     0.504643
TTC34      5      69     41     186      0.02422    0.3297116
ZXDC       8      66     37     190       0.3474    0.6233377
ASAH2      9      65     46     181       0.1648    0.5458072

我不知道该怎么做。有人能帮忙吗?谢谢

您可以使用
矩阵将每行转换为列联表

ft.res <- apply(data, 1, function(x){
    t1 <- fisher.test(matrix(x, nrow = 2))
    data.frame(p_value = t1$p.value, odds_ratio = t1$estimate)
})

cbind(data, do.call(rbind, ft.res))
#         R_T1 R_T2 NR_T1 NR_T2    p_value odds_ratio
# GMNN      14   60    70   157 0.05273179  0.5243787
# GORASP2    7   67    39   188 0.13671487  0.5046430
# TTC34      5   69    41   186 0.02421765  0.3297116
# ZXDC       8   66    37   190 0.34744964  0.6233377
# ASAH2      9   65    46   181 0.16478480  0.5458072

ft.res您可以使用
apply
,在数据帧的行中循环:

## Replicating the data
d  = data.frame(R_T1=c(14,7,5,8,9),R_T2=c(60,67,69,66,65),NR_T1=c(70,39,41,37,46),NR_T2=c(157,188,186,190,181))
row.names(d) = c("GMNN","GORASP2","TTC34","ZXDC","ASAH2")
## Computing the fisher test and getting the values for each row 
d[,c("p_value","odds_ratio")] = t(apply(d,1,function(x) {f=fisher.test(matrix(x,2,2));c(f$p.value,f$estimate)}

        R_T1 R_T2 NR_T1 NR_T2    p_value odds_ratio
GMNN      14   60    70   157 0.05273179  0.5243787
GORASP2    7   67    39   188 0.13671487  0.5046430
TTC34      5   69    41   186 0.02421765  0.3297116
ZXDC       8   66    37   190 0.34744964  0.6233377
ASAH2      9   65    46   181 0.16478480  0.5458072

下面介绍了如何使用
dplyr
mutate
使用
rowwise

df <- read.table(text="rowname R_T1   R_T2    NR_T1  NR_T2
GMNN      14      60     70     157
GORASP2    7      67     39     188
TTC34      5      69     41     186
ZXDC       8      66     37     190
ASAH2      9      65     46     181",
header=TRUE,stringsAsFactors = FALSE)

df%>%
rowwise%>%
mutate(p.value=fisher.test(matrix(c(R_T1,R_T2,NR_T1,NR_T2),nrow=2))$p.value,
       odds_ratio=fisher.test(matrix(c(R_T1,R_T2,NR_T1,NR_T2),nrow=2))$estimate)

  rowname  R_T1  R_T2 NR_T1 NR_T2    p.value odds_ratio
    <chr> <int> <int> <int> <int>      <dbl>      <dbl>
1    GMNN    14    60    70   157 0.05273179  0.5243787
2 GORASP2     7    67    39   188 0.13671487  0.5046430
3   TTC34     5    69    41   186 0.02421765  0.3297116
4    ZXDC     8    66    37   190 0.34744964  0.6233377
5   ASAH2     9    65    46   181 0.16478480  0.5458072
df%
行%>%
变异(p.value=fisher.test(矩阵(c(R_T1,R_T2,NR_T1,NR_T2),nrow=2))$p.value,
比值比=fisher检验(矩阵(c(R_T1,R_T2,NR_T1,NR_T2),nrow=2))$估计)
rowname R_T1 R_T2 NR_T1 NR_T2 p.值比值比
1 GMNN 14 60 70 157 0.05273179 0.5243787
2 GORASP2 7 67 39 188 0.13671487 0.5046430
3 TTC34 5 69 41 186 0.02421765 0.3297116
4 ZXDC 8 66 37 190 0.34744964 0.6233377
5 ASAH2 9 65 46 181 0.16478480 0.5458072