-3

I have over 5000 data sets that i want to find p values for using fishers exact test in R. They are saved in a csv file and look something like this;

100 5000 400 500
250 400  600 400


... ... ... ...

where each row represents a contingency table. Right now, I'm having to do a contingency table at a time, which will take me forever.

I used this code so far alltables<-read.table("untitled1.csv") ##to read my data apply(alltables,1, function(x) fisher.test(matrix(x,nr=2))$p.value)

But then I get the error "Error in fisher.test(matrix(x, nr = 2)) : 'x' must have at least 2 rows and columns"

user3796053
  • 11
  • 1
  • 4
  • Could you share any code you're written so far to address this and where you've gotten stuck? – josliber Jul 09 '14 at 05:37
  • sure. I read in my data using ` alltables<-read.table("untitled1.csv")` then tried to use the apply function ` apply(alltables,1, function(x)` ` fisher.test(matrix(x,nr=2))$p.value)` But then I get the error "Error in fisher.test(matrix(x, nr = 2)) : 'x' must have at least 2 rows and columns" – user3796053 Jul 09 '14 at 05:52
  • Great -- please edit your question to include this important information (both the code you've run and the error). Ideally you would also include a small reproducible example that demonstrates your issue (remember that others don't have access to untitled1.csv). – josliber Jul 09 '14 at 06:03

1 Answers1

0

You can do something like the following. But since you didn't really give a reproducible example, I first create some toy-data:

set.seed(1)
print(dat <- matrix(rbinom(n = 40, size = 1000, prob = 0.5), ncol = 4))
#      [,1] [,2] [,3] [,4]
# [1,]  500  526  494  505
# [2,]  497  500  512  493
# [3,]  480  488  500  512
# [4,]  464  513  498  497
# [5,]  527  503  518  508
# [6,]  504  517  511  483
# [7,]  519  493  522  471
# [8,]  486  490  497  507
# [9,]  492  499  475  509
#[10,]  530  486  488  501

# Function to be applied row-wise
rowFisher <- function(x, ...) {
  return(fisher.test(matrix(x, nrow = 2, ...))$p.value)
}

# Apply the function row-wise
apply(dat, 1, rowFisher)
# [1] 0.7557946 0.6548804 0.9641424 0.2603181 0.7912598 0.3729036 0.5916955 0.9283668 0.5585135
#[10] 0.2111895

Edit I didn't see your commments. But this should do the trick. If not, probably you have some NAs or other non-numeric values somewhere in your data.

Anders Ellern Bilgrau
  • 9,928
  • 1
  • 30
  • 37
  • sorry about the sketchy data and thank you for the input. I was able to correct the mistake in my original code. I needed to use "read.csv" and not "read.table" to read my data into R. – user3796053 Jul 10 '14 at 05:04