TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
Is this a pairwise Wilcoxon test? If so, check out pairwise.wilcox.test. – Aaron Apr 5 '11 at 13:42
@Aaron: hello , i already made the test and i have the result , but how can i put them inside a data frame and then generate a latex table from this data frame and output it as PDF?? – weblover Apr 5 '11 at 13:50
I'll trust that this is appropriate for your case, but it seems very odd to jitter a variable before running a statistical test on it. Also, without the jitter, your function simply recreates the pairwise wilcoxon test that's built into R. – Aaron Apr 5 '11 at 14:15
concerning the jitter , i'm using it because if i didn't all the p-values will be the same value equal to 1. i read this in a tutorial , it can avoid ties. is this will give wrong result?? – weblover Apr 5 '11 at 14:47
@weblover You're better off asking directly on stats.stackexchange.com. I don't know about your sample size, but there may exist better alternatives like permutation tests, depending on your specific question. Also, your R function looks unnecessarily complicated. We can help for the tex/pdf output too, but I feel you should get back here with the stats response, as your original question belongs on here. (Be kind enough to cross-link the two threads.) – chl Apr 5 '11 at 15:23
up vote 0 down vote accepted

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 the out 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.

<<echo=FALSE, results=hide>>=
# make up some data
N <- 15
t <- data.frame(a=round(runif(N)*100), 
uni <-  unique(c(t$V9))

# function to get result for a particular data column
testresult<- function(column, datacol) {    
  out <- NULL
  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]]))
        out <- rbind(out, data.frame(xp=xp, yp=yp, p.value=zz$p.value))

# get results for columns a and b
ta <- testresult("PP","a")
tb <- testresult("PP","b")

# load xtable library for output

\section*{a results}
<<results=tex, echo=FALSE>>=
print(xtable(ta), include.rownames=FALSE, floating=FALSE)

\section*{b results}
<<results=tex, echo=FALSE>>=
print(xtable(tb), include.rownames=FALSE, floating=FALSE)


If you'd rather not Sweave, you could do something like what the latex function of Hmisc 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 table tab, outputting the results to a file with basename file.

pdfit <- function(tab, file) {
  texfile <- paste(file, ".tex", sep="")
  cat("\\documentclass{article}\n\\begin{document}\n", file=texfile)
  print(xtable(tab), include.rownames=FALSE, floating=FALSE, 
        file=texfile, append=TRUE)
  cat("\\end{document}\n", file=texfile, append=TRUE)
  sys(paste("pdflatex", texfile))
pdfit(ta, "ta")
share|improve this answer
@Aaron : thnx a lot for your help, but i don't want to use sweave , i just want to output the latex table generated for ta and tb in a PDF file. what should i add in this case?? and how to control the lines separating variables of the table inside R?? – weblover Apr 5 '11 at 14:16
I don't understand your question; if you don't want to use Sweave, just run the R code in R and copy/paste, as I said. If I misunderstood your question, please rephrase. – Aaron Apr 5 '11 at 14:22
@Aaron: this is just 1 PDF file , i can copy the output and place it inside latex , but i have another case that will generate around 25 PDF , i can't copy and paste :S , soo is there a case to integrate latex with R as i wanted ?? (to automatically generate PDF from latex tables inside the for loop) , thanks in advance – weblover Apr 5 '11 at 14:29
I'd make one pdf file and then split it with pdftk; something like pdftk largepdfile.pdf burst. – Aaron Apr 5 '11 at 17:49
You also might want to look at the latex function in the Hmisc package; I think it will just make dvi files but you could do something similarly to make pdfs. I'll add a note to my answer too. – Aaron Apr 5 '11 at 19:09

If you can save the table to a text file, I think you can import it in LaTeX with the pgfplotstable package. From its documentation:

PgfplotsTable is a lightweight sub-package of pgfplots which employs its table input methods and the number formatting techniques to convert tab-separated tables into tabulars.

Its input is a text file containing space separated rows, possibly starting with column names. Its output is a LaTeX tabular which contains selected columns of the text table, rounded to the desired precision, printed in the desired number format (fixed point, integer, scientific etc.).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.