0
votes

I have a csv format contingency table made by python language , like this:

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

now i want to derive 2x2 contingency tables from this contingency table to calculate chi-square value using R

how can i derive a 2x2 table like the following from the contingency table above:

            case  control
disease_A    20    30 
disease_D    52    62

Thats probably a novice question but im new to R and i couldn't find the solution anywhere else

2

2 Answers

1
votes

Here's an approach.

The data:

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

Read the data:

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

Extract a subset of rows:

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

If M is of class 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

If M is a data.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)),]))