1

I have the table with alternative and reference allele counts. How could I loop chi sq test in R to run it for each row? I attached the picture of my table. I need to perform chi sq test with altCount and refCount columns.

   altCount refCount
     8        6
     3        7
     4        9 

I need the p-value for each row. I am very new to R. I figured out how to perform chi sq test on individual rows but since I have several thousands rows I need to make a loop to run it all at once. I did:

bcz <- read.delim("C:/cygwin64/home/sbomb/tables/bc_z_alleles.csv")
a = bcz[1,6]
b = bcz[1,7]
c = c(a,b)
d = c(0.5,0.5)
chisq.test(
     x = c,
     p = d, 
 )

but I do not know how to loop it for whole table. Could you please explain me how to do it with all details?

enter image description here

Sergey Bombin
  • 45
  • 1
  • 6
  • Posting pictures of data isn't very helpful. Try to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) in the question itself which gives sample input and clearly shows the desired output. – MrFlick Sep 22 '16 at 19:44
  • as mr. Flick explained, you need to show data. saying this, I think this example may give you lead to try and then post if you cannot solve yourself. This time with reproducible example. Good luck http://stackoverflow.com/questions/25350618/run-chi-squared-test-on-a-data-frame. – user5249203 Sep 22 '16 at 19:53

1 Answers1

0

The easiest loop for a beginner still is the for loop:

d <- data.frame(a = c(8,3,4), b = c(6,7,9))
for(row in 1:nrow(d)){
    print(row)
    print(chisq.test(c(d[row,1],d[row,2])))
}

The same can be done with

apply(d, 1, chisq.test)

The latter is shorter and gives you a list as your result, which is probalby better for further evaluation.

Bernhard
  • 4,272
  • 1
  • 13
  • 23
  • Thank you for your advise but what should I do if in my data table the number of rows are 44,850, so I cannot just retype each value like a = c(8,3,4)? – Sergey Bombin Sep 22 '16 at 20:44
  • I think I found the answer. If I type: d <- data.frame(a = test1[,1], b = test1[,2]) it works correctly. – Sergey Bombin Sep 23 '16 at 00:18