Skip to content

“Pretty” table columns 2

I recently wrote a few functions for making some common column formats and posted them here. I’ve recently added them to github gist so that you can download them straight into R using


I changed a couple of the function names though and added an argument to specify_decimal so that you can provide a minimum value. The function arguments are now:

specify_decimal(x, k, min)
npropconv(n, perc, dp.n, dp.perc)
valciconv(val, CIlower, CIupper, dp.val, dp.CI)
minval(values, min)

minval is the new name for minp.

Hope thats useful…



“Pretty” table columns

Every now and then you might want to make a nice table to include directly in your documents without having to faff about with columns later in excel or word. Typical issues might be the specification of decimal places, converting a value and proportion/SE column into one to take the form of n (x) or a value and CIs into x (x_min – x_max). I needed to do these recently and wrote the following functions to make doing it a bit faster and more productive…

specify_decimal <- function(x, dp){
 out <- format(round(x, dp), nsmall=dp)
 out <- gsub(" ", "", out)

npropconv <- function(n, perc, dp.n=0, dp.perc=1){
 n <- specify_decimal(n, dp.n)
 perc <- specify_decimal(perc, dp.perc)
 OUT <- paste(n, " (", perc, ")", sep="")
valciconv <- function(val, CIlower, CIupper, dp.val=2, dp.CI=2){
 val <- specify_decimal(val, dp.val)
 CIlower <- specify_decimal(CIlower, dp.CI)
 CIupper <- specify_decimal(CIupper, dp.CI)
 OUT <- paste(val, " (", CIlower, " - ", CIupper, ")", sep="")
minp <- function(pvalues, min=0.001){
  pv <- as.numeric(pvalues)
  for(i in 1:length(pv)){
    if(pv[i] < min) pvalues[i] <- paste("<", min)

And heres what they do…

> specify_decimal(x=c(0.01, 0.000001), dp=3)
[1] "0.010" "0.000"
> npropconv(n=7, perc=5, dp.n=0, dp.perc=1)
[1] "7 (5.0)"
> valciconv(val=7, CIlower=3, CIupper=9, dp.val=2, dp.CI=2)
[1] "7.00 (3.00 - 9.00)"
> minp(0.00002, min=0.05)
[1] "< 0.05"
> minp(0.00002, min=0.001)
[1] "< 0.001"

Any arguments with beginning with dp specify the number of decimal places for the relevant parameter (i.e. dp.n sets the decimal places for the n parameter). It would make sence to follow specify_decimal with a call to minp to deal with the 0s:

> decim <- specify_decimal(c(0.01, 0.000001),3)
> minp(decim, 0.001)
[1] "0.010"   "< 0.001"


Incidently, although I had p values in mind for minp, it can of course be used with any value at all!

Hope someone finds them handy!!!

Calculating confidence intervals for proportions

Heres a couple of functions for calculating the confidence intervals for proportions.

UPDATE: These confidence intervals, together with many more, have actually been programmed in the binom package (binom.confint). Use them instead. For Stata users, CIs from proportions are available with the ci program.

Firstly I give you the Simple Asymtotic Method:

simpasym <- function(n, p, z=1.96, cc=TRUE){
  out <- list()
    out$lb <- p - z*sqrt((p*(1-p))/n) - 0.5/n
    out$ub <- p + z*sqrt((p*(1-p))/n) + 0.5/n
  } else {
    out$lb <- p - z*sqrt((p*(1-p))/n)
    out$ub <- p + z*sqrt((p*(1-p))/n)

which can be used thusly….

simpasym(n=30, p=0.3, z=1.96, cc=TRUE)
[1] 0.119348

[1] 0.480652


Where n is the sample size, p is the proportion, z is the z value for the % interval (i.e. 1.96 provides the 95% CI) and cc is whether a continuity correction should be applied. The returned results are the lower boundary ($lb) and the upper boundary ($ub).

The second method is the Score method and is define as follows:

scoreint <- function(n, p, z=1.96, cc=TRUE){
  out <- list()
  q <- 1-p
  zsq <- z^2
  denom <- (2*(n+zsq))
    numl <- (2*n*p)+zsq-1-(z*sqrt(zsq-2-(1/n)+4*p*((n*q)+1)))
    numu <- (2*n*p)+zsq+1+(z*sqrt(zsq+2-(1/n)+4*p*((n*q)-1)))
    out$lb <- numl/denom
    out$ub <- numu/denom
    if(p==1) out$ub <- 1
    if(p==0) out$lb <- 0
  } else {
    out$lb <- ((2*n*p)+zsq-(z*sqrt(zsq+(4*n*p*q))))/denom
    out$ub <- ((2*n*p)+zsq+(z*sqrt(zsq+(4*n*p*q))))/denom

and is used in the same manner as simpasym…

scoreint(n=30, p=0.3, z=1.96, cc=TRUE)
[1] 0.1541262

[1] 0.4955824

These formulae (and a couple of others) are discussed in Newcombe, R. G. (1998) who suggests that the score method should be more frequently available in statistical software packages.

Hope that help someone!!!


Newcombe, R. G. (1998) Two-sided confidence intervals for the single proportion: comparison of seven methods. Statist. Med., 17: 857-872. doi: 10.1002/(SICI)1097-0258(19980430)17:8<857::AID-SIM777>3.0.C


Cropping figures in LaTeX

Sometimes when you include a figure, you dont want to have to edit the picture file itself. In MS Word for instance you can use the crop command. LaTeX being LaTeX doesnt work quite the same way, but it is still possible. The standard way is to use the trim and clip options to \includegraphics

\includegraphics[trim= left bottom right top, clip=true]{file.jpeg}

This method doesnt always work however. For instance, if you use xelatex, like me.

There is a way around this though – the adjustbox package. It includes, among other handy functions, the function \adjincludegraphics which does the trick. One of the best things about it is that it even seems to use the same coding, so no need to change all of the argument names…just add adj to the function call.

\adjincludegraphics[trim= left bottom right top, clip=true]{file.jpeg}

I think that its even possible to have it export its settings so that \includegraphics is recoded to do the same as \adjincludegraphics. I’ve not yet tried that though. (It should be just a case of using \usepackage[Export]{adjustbox} instead of \usepackage{adjustbox})

Changing figure or table number styles in LaTeX

Changing how LaTeX prints table or figure numbers is surprisingly easy to do. Say you have a figure and rather that the figure being labelled as Figure 4 you want it to be S1 because its a supplementary figure.


So we have reset the figure counter to zero and have told LaTeX to redefine \thefigure to have an S before the figure number. Easy huh!?!

For documents with chapters it is almost as easy to do:


To change tables instead of figures, just swap any instances of figure for table. Piece of cake.

Do remember to reset it though, otherwise your next chapter continues to use the changes you’ve made!!

lme4 version 1 out soon!

Just a quick post to say that on Saturday Ben Bolker (on behalf of the lme4 authors) announced that lme4 version 1 will be released to CRAN on the 1st August.

So to anyone thats written code based on the current lme4 that uses the @ to access parts of the model object…its time to start rethinking those functions…

Boolean in LaTeX macros

Much like in the statistical environment R, it is possible to make custom functions (or “macros”) in LaTeX. This is done using the


command, where commandname and definition are replaced with a name (e.g. \mysubscript) and a definition, respectively, and number_of_arguments can be provided or not. For example see my previous post on subscripts.

What do you do if you want to control the behaviour of that macro? Say you want the command to do one of two things, print Fig or Figs for instance.

One way to accomplish this is to use the etoolbox package:

\fig{} is singular, while \fig{1} is plural.

All this does is checks to see if there is an argument (#1) and prints its second argument if not (spaces or blank), or its third argument if there is something.

Hope this helps someone!

[UPDATE] A better method seems to be to use the following


otherwise it sometimes adds in extra space for no apparent (to me) reason. I’m not sure what the \makeatletter and \makeatother lines are about, but the other parts define a command called \fig which if defined as “if the next character is a [ do \@with else do \@without“.  \@with and \@without are then defined separately.



Get every new post delivered to your Inbox.