R 如何对表进行子集划分并使用值进行统计计算

R 如何对表进行子集划分并使用值进行统计计算,r,statistics,R,Statistics,我有一个数据框,看起来像这样: 表1 对于每一行,我想使用需要2x2表的fisher.test()函数计算优势比和95%置信区间。当我手动提供数字来进行测试时,它运行良好:fisher.test(矩阵(c(17,116,78,141),ncol=2,byrow=TRUE)) 但是当我做fisher.test(矩阵(c(表1[1,2:5]),ncol=2,byrow=T))时,它给了我这个错误: “x”的所有条目必须是非负且有限的 请说明我做错了什么您没有向函数传递正确的输入。当你这样做的时候,它

我有一个数据框,看起来像这样:

表1

对于每一行,我想使用需要2x2表的
fisher.test()
函数计算优势比和95%置信区间。当我手动提供数字来进行测试时,它运行良好:
fisher.test(矩阵(c(17,116,78,141),ncol=2,byrow=TRUE))
但是当我做fisher.test(矩阵(c(表1[1,2:5]),ncol=2,byrow=T))时,它给了我这个错误:

“x”的所有条目必须是非负且有限的


请说明我做错了什么

您没有向函数传递正确的输入。当你这样做的时候,它工作得很好。例如:

dat <- read.table(text="SNo. C1   C2 C3  C4 C5  
1. A*01   17 116 78 141
2. A*02   30 1   65 256",header=TRUE)

apply(
  dat[-(1:2)],
  1,
  function(x) fisher.test(matrix(x,nrow=2,byrow=TRUE)) 
)

#[[1]]
#
#        Fisher's Exact Test for Count Data
#...
#odds ratio 
# 0.2658521 
#
#
#[[2]]
#
#        Fisher's Exact Test for Count Data
#...
#odds ratio 
#  116.4649 

dat您没有向函数传递正确的输入。当你这样做的时候,它工作得很好。例如:

dat <- read.table(text="SNo. C1   C2 C3  C4 C5  
1. A*01   17 116 78 141
2. A*02   30 1   65 256",header=TRUE)

apply(
  dat[-(1:2)],
  1,
  function(x) fisher.test(matrix(x,nrow=2,byrow=TRUE)) 
)

#[[1]]
#
#        Fisher's Exact Test for Count Data
#...
#odds ratio 
# 0.2658521 
#
#
#[[2]]
#
#        Fisher's Exact Test for Count Data
#...
#odds ratio 
#  116.4649 
dat检查
c(表1[1,2:5])
product.检查
c(表1[1,2:5])
product.打败我!:)回答得好。打败我!:)回答得好。