I wrote a script that generates 4 variables from 2 for loops, each one of them contain a big number of data. I want to place these variables inside a data frame and then create a latex table so I can output it as a PDF.
t <- read.table("data.txt", stringsAsFactors=FALSE)
uni <- unique(c(t$V9))
testresult <- function(column, datacol) {
if(column=="PP") {
for (xp in uni) {
for(yp in uni) {
testx <- subset(t, V9==xp)
testy <- subset(t, V9==yp)
zz <- wilcox.test(testx[[datacol]],jitter(testy[[datacol]]))
p.value <- zz$p.value
}
}
}
}
I want the table to contain these 4 columns (xp, yp, p-value, significance) for each xp and yp in the 2 for loops.
The question about whether the jitter in my function is appropriate has been taken to stats.stackexchange.com:
is it wrong to jitter before performing wilcox test
Best Answer
To make a data frame with your results, use
rbind
to add the results by row to an output object within the loop, as shown with theout
object below.To get a LaTeX table from the data frame, use
xtable
.The easiest way to make all this happen is with an Sweave document, like the following. If you don't want to use Sweave, you can just run the R code and copy/paste the resulting LaTeX tables into your own LaTeX document.
If you'd rather not Sweave, you could do something like what the
latex
function ofHmisc
does and create the .tex file from within R and then do a system call to pdflatex. Here's a function that would do that on a tabletab
, outputting the results to a file with basenamefile
.