“Pretty” table columns

April 10, 2014
By

(This article was first published on Insights of a PhD student » R, and kindly contributed to R-bloggers)

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)
 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="")
 OUT
 }
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="")
 OUT
 }
minp <- function(pvalues, min=0.001){
  pv <- as.numeric(pvalues)
  for(i in 1:length(pv)){
    if(pv[i] < min) pvalues[i] <- paste("<", min)
  }
  pvalues
}

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!!!

To leave a comment for the author, please follow the link and comment on their blog: Insights of a PhD student » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Mango solutions



RStudio homepage



Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training



http://www.eoda.de







ODSC

ODSC

CRC R books series











Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)