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