[R] Converting tables to matrices

Michael Bedward michael.bedward at gmail.com
Fri Sep 17 07:03:20 CEST 2010


Hello Selthy,

Here's one way. Assume your tables are called x.trt and x.cont

# form a single matrix
x <- cbind(x.trt, x.cont)
colnames(x) <- c("Ntrt", "BPtrt", "Ncon", "BPcon")
result <- apply( x, 1, function(xrow) fisher.test( matrix(xrow, nrow=2) ) )

Now result is a list where element i holds the test results for row i
of your data. E.g. to get the results for row 1..

results[[1]]

Michael


On 17 September 2010 14:04, selthy <selthy at hotmail.com> wrote:
>
> Hi there,
> I'm a real beginner to R. I have two tables of the following format (~1000
> rows in each):
> Table 1 (experimental)
> N        bp
> 1064   12312089
> 856     12312234
> ....
>
> Table 2 (control)
> 8463    34728908
> 879      34443290
> .....
>
> I would like to do Fisher's exact tests comparing each row of the tables
> e.g. the test for row 1 would look something like this:
>     Experimental    Control
> N    1064              8463
> bp  12312089        34728908
>
> How do I either:
> a) Create ~1000 2x2 matrices from each row of the two tables?, or
> b) Use the fisher.test function repeatedly for each row of the two tables?
>
> Thanks for our help!
> Selthy
> --
> View this message in context: http://r.789695.n4.nabble.com/Converting-tables-to-matrices-tp2543309p2543309.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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