4

I use the datatool package to read CSV files and print them as tables. Before printing I want to sort the rows. The problem is that the file was written with R and some rows contain normal numbers (like 0.124) and some contain numbers in scientific format (e.g. 1.23e-4).

How can I tell datatool to interpret both values as numerical and sort accordingly?

  • Can you provide a MWE? – karlkoeller Aug 22 '13 at 14:14
  • Do you know Sweave and xtable in R? – Fran Aug 23 '13 at 1:01
5

You don't need datatool, at least for this work. You can sort the data without showing scientific notation and make a beautiful table just inserting some R code in your LaTeX file and compile with Sweave (or knitr).

To do in this way, you LaTeX document must have a .Rnw extension and compile in two steps with:

R CMD Sweave MWE.Rnw % this make MWE.tex 
pdflatex MWE.tex     % this make MWE.pdf

If you file have crossreferences, as usual, you must repeat the pdflatex compilation. If only a pdflatex run is needed, you can make all with R:

R CMD Sweave --pdf  MWE.Rnw % this make the MWE.pdf

(Note: RStudio understand .Rnw files and make all these steps with one click.)

This is the example: (the external data source is typed verbatim, you can copy this part and save as text.csv to test the code):

\documentclass[spanish,11pt]{article}
\begin{document}
\setlength{\parindent}{0pt}
\SweaveOpts{concordance=TRUE}

Raw file \texttt{text.csv}: 

\begin{verbatim}
minor, big
0.34552, 25
1.23e-4, 34
5.45677223344, 12
0.000001, 99
\end{verbatim}

R display in plain text by default 
<<RawTable,echo=F>>=
a  <- read.table("text.csv", header=TRUE, sep=",")
a
@
R display ordered and rounded:
<<RawTable,echo=F>>=
round(a[order(a$minor), ],4)
@
In R also but with  \LaTeX{} style
<<Mytable,echo=F,results=tex>>=
library(xtable)
xtable(a[order(a$minor), ], caption="My ordered table",digits=4)
@
\end{document}

MWE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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