0

I am performing chisq.test on very large data table and trying to send my output to the separate column that would be joint to the main table. The mini version of my table looks like:

 altCount refCount
     8        6
     3        7
     4        9 

I wrote a script that will loop chisq.test and sent output to separate text file but I cannot sent it as the separate column. My script is:

sink("output.txt", append=FALSE, split=FALSE)
d <- data.frame(a = test1[,1], b = test1[,2])
d$newcolumn <- 0
for(row in 1:nrow(d)){
    print(row)
    print(chisq.test(c(d[row,1],d[row,2])))
    d$newcolumn[row] <- 3
}

I found the similar question in: For loop R create and populate new column with output As the output I am getting file that looks like this:

data:  c(d[row, 1], d[row, 2])
X-squared = 0.28571, df = 1, p-value = 0.593

But I want to Output look like:

altCount refCount  P-val
     8        6    0.564
     3        7    0.03
     4        9    0.005

So, basically same table but with new column with p-values

I am very new to R. Could you please explain in details what I am doing wrong? More details

Community
  • 1
  • 1
Sergey Bombin
  • 45
  • 1
  • 6

1 Answers1

0

The error tells you that the object i cannot be found. This did not mean that R did not look good enough, it means that the variable i does not exist. You should make sure the i exist.

Your for loops over the numbers between 1 to nrow(d) and assigns the value of it each loop to row. It is highly likely that you mixed up the variable row and the variable i. The code that you presented should work:

for(row in 1:nrow(d)){
    print(row)
    print(chisq.test(c(d[row,1],d[row,2])))
    d$newcolumn[row] <- 3
}
Pieter
  • 3,262
  • 1
  • 17
  • 27