从R中更大的列联表中导出列联表

时间:2014-08-07 06:52:30

标签: r contingency

我有一个由python语言制作的csv格式列联表,如下所示:

            case  control
disease_A    20    30 
disease_B    35    45
disease_C    42    52
disease_D    52    62

现在我想从这个列联表中导出2x2列联表,用R

计算卡方值

如何从上面的列联表中导出如下所示的2x2表:

            case  control
disease_A    20    30 
disease_D    52    62

这可能是一个新手问题,但我是R的新手,我无法在其他任何地方找到解决方案

2 个答案:

答案 0 :(得分:1)

这是一种方法。

数据:

txt <-  "           case  control
disease_A    20    30 
disease_B    35    45
disease_C    42    52
disease_D    52    62"

阅读数据:

dat <- read.table(textConnection(txt))
#           case control
# disease_A   20      30
# disease_B   35      45
# disease_C   42      52
# disease_D   52      62

提取行的子集:

dat2 <- dat[rownames(dat) %in% c("disease_A", "disease_D"), ]
#           case control 
# disease_A   20      30
# disease_D   52      62

答案 1 :(得分:0)

如果M属于table

M <- structure(c(20, 35, 42, 52, 30, 45, 52, 62), .Dim = c(4L, 2L), .Dimnames = list(
c("disease_A", "disease_B", "disease_C", "disease_D"), c("case", 
"control")), class = "table")



xtabs(Freq~Var1+Var2,data= subset(as.data.frame(M,stringsAsFactors=F),
                   Var1%in% c("disease_A", "disease_D")))
           Var2
 Var1        case control
  disease_A   20      30
  disease_D   52      62

如果Mdata.frame

 M <- structure(list(case = c(20L, 35L, 42L, 52L), control = c(30L, 
 45L, 52L, 62L)), .Names = c("case", "control"), class = "data.frame", row.names =   c("disease_A", 
 "disease_B", "disease_C", "disease_D"))

 as.table(as.matrix(M[grep("A|D", rownames(M)),]))