[R] Reshape into a contingency table/Fisher's test

Jacques VESLOT jacques.veslot at good.ibl.fr
Tue Oct 3 15:37:00 CEST 2006


 > test <- read.table("clipboard",sep=";",row=1,header=T, as.is=T)
 > test
            X00 X01 X10 X11
John.Mike  123 313  12  31
John.Jim    54  57  39  36
John.Steve 135  47  47  74
Mike.Jim    63  37  27  16
Mike.Steve  15  15   5  61
Jim.Steve    6  10  34  35

 > list.mat <- mapply(function(x,y) structure(matrix(x,2,2), dimnames=list(unlist(strsplit(y, ".", 
fixed=T)), 0:1)), as.data.frame(t(test)), rownames(test), SIMPLIFY=F)
 >
 > list.mat
[[1]]
        0  1
John 123 12
Mike 313 31

[[2]]
       0  1
John 54 39
Jim  57 36

[[3]]
         0  1
John  135 47
Steve  47 74

[[4]]
       0  1
Mike 63 27
Jim  37 16

[[5]]
        0  1
Mike  15  5
Steve 15 61

[[6]]
        0  1
Jim    6 34
Steve 10 35

 >
 > cbind(rownames(test), sapply(list.mat, function(x) sprintf("%.G", fisher.test(x)$p.value)))
      [,1]         [,2]
[1,] "John.Mike"  "1"
[2,] "John.Jim"   "0.8"
[3,] "John.Steve" "1E-09"
[4,] "Mike.Jim"   "1"
[5,] "Mike.Steve" "7E-06"
[6,] "Jim.Steve"  "0.4"

-------------------------------------------------------------------
Jacques VESLOT

CNRS UMR 8090
I.B.L (2ème étage)
1 rue du Professeur Calmette
B.P. 245
59019 Lille Cedex

Tel : 33 (0)3.20.87.10.44
Fax : 33 (0)3.20.87.10.31

http://www-good.ibl.fr
-------------------------------------------------------------------


Serguei Kaniovski a écrit :
> Dear all,
> 
> how can I "reshape"/"cast" the following matrix
> 
> 00;01;10;11
> John.Mike;123;313;12;31
> John.Jim;54;57;39;36
> John.Steve;135;47;47;74
> Mike.Jim;63;37;27;16
> Mike.Steve;15;15;5;61
> Jim.Steve;6;10;34;35
> 
> into a set of stacked 2x2 contingency tables
> 
> 0;1
> John;123;12
> Mike;313;31
> John;54;39
> Jim;57;36
> John;135;47
> Steve;47;16
> ...
> 
> so that the "fisher.test" and "chisq.test" can be "applied" to the
> pairs of names. As as result I would like to have the original format, ie
> 
> John.Mike
> John.Jim
> John.Steve
> Mike.Jim
> Mike.Steve
> Jim.Steve
> 
> with two columns containing the p-values of the tests.
> 
> Thanks a lot,
> Serguei
> --
> ___________________________________________________________________
> 
> Austrian Institute of Economic Research (WIFO)
> 
> Name: Serguei Kaniovski			P.O.Box 91
> Tel.: +43-1-7982601-231			Arsenal Objekt 20
> Fax:  +43-1-7989386			1103 Vienna, Austria
> Mail: Serguei.Kaniovski at wifo.ac.at
> 
> http://www.wifo.ac.at/Serguei.Kaniovski
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list