Tuesday 25 June 2013

Making (kind of) good-looking data tables

Unavoidable task in writing up any scientific paper is to prepare a data table, or rather several of them.  You will probably agree that it quite easy to waste a lot of time trying to format such a table so that it complies with in-house rules of the given Journal or just (often worse) our own needs/wishes. Most commonly, we would like to arrange the analyses according to some key, for instance grouping together the analyses coming from the same intrusion or locality. Moreover, we may want to order the analyses within each of the groups according to an additional criterion, say the increasing silica contents. Lastly, the page width is finite and thus there is likely to be a limit on number of analyses we can fit across a page.
Fortunately the new GCDkit comes with a function which makes this ask easier. In fact, it is currently not attached to the menu system and this is exactly the moment when it is good to be a power user, i.e. not afraid to type in a few lines of code. ;-)

The function in question is called HTMLtableOrdered and outputs the data stored in the numeric matrix specified by parameter 'what'. Optional argument 'which' gives the list of sample names (rows) in the matrix to be saved. The data are first sorted based on 'key2', which typically gives a grouping information (name of a column in 'labs'). Within each of the groups, the data are further sorted based on the numeric variable 'key1'. And the number of analyses across a page is indicated by the parameter 'split.by'. For example, we can produce a table of Large Ion Lithophile Elements (LILE) from our sample datafile, sazava.data:
# Read in the sample data
title="LILE (ppm)",split.by=7)
There are some other parameters which can be included, for instance the table can have samples in rows, as we are used to from the GCDkit system, and the analyses summed up:

title="Major-element data (wt. %)",split.by=14,rotate=FALSE,sum.up=TRUE)
And here is the last example, generating the table from results, here of the CIPW norm calculation.
title="CIPW norm",split.by=5)

I just hope you have found the today's tip useful and that it will save you some time to do something more sensible than formatting tables.......


  1. I was wondering why you do not provide support for GNU/Linux users, because your project seems to be conforming with GNU philosophy... So I would be very pleased to use your fantastic software on my Debian Pc.


  2. Hi!
    I'm using the newest version of both GCDkit and R and I have problems with creating tables with these codes above. I successfuly read in the sazava file, but when I typed (or rather copy, paste :) any of these HTMLtableOrdered codes, I got this:
    error in file(file, ifelse(append "a" "w" )) : cannot open the connection
    What does it mean?


    1. Hello,
      it seems like some problem with writing permission. Can you please try whether the problem persists when you run GCDkit as an administrator? Another thing to try - you can change the working directory from R menu File|Change dir... before loading data and trying to make any tables.

      I trust that this helps,