1

I am using a combination of xtable, rtable and ReporteRs to export summary tables of various objects (aov, glm's, lm's etc) to Latex/Word/Powerpoint. I'm not so fond of the default way, however, in which p values are given with a fixed number of digits after the comma. Instead I would prefer that it would give me p values in a similar format as one would get with base R function format.pval() with digits=2, i.e. give p values rounded to two significant digits - in this case 4.2e-17 and 0.25).

E.g.

ctl = c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt = c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group = gl(2, 10, 20, labels = c("Ctl","Trt"))
weight = c(ctl, trt)
fit = lm(weight ~ group)
s=summary(fit)
s

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   4.8465     0.1557  31.124   <2e-16 ***
group1        0.1855     0.1557   1.191    0.249    

library(xtable)
library(rtable)
library(ReporteRs)
tab=as.FlexTable(xtable(s))
tab

enter image description here

I would like to have the p-values formatted in a similar way as in summary(fit), however (right aligned, rounded to two significant digits, in this case 4.2e-17 and 0.25). Anybody any idea how this could be achieved in a generic way, ideally for the p values given by all the objects supported by xtable?

EDIT: with the help below I now made a [small package export] with helper functions table2doc, table2ppt and table2html to export the previously shown R stats object to Word, Powerpoint or HTML (and with function graph2ppt and graph2doc to export the currently active graph to Powerpoint or Word), see https://cran.r-project.org/web/packages/export/index.html and https://github.com/tomwenseleers/export

Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103
  • what about `xtable(s, digits = c(4,4,4,2,2), display = c('f','f','f','f','g'))` – rawr Jul 07 '15 at 22:47
  • Thx for that - this goes some way towards solving the problem, although the next problem would then be to redefine these default params for all the objects that xtable can deal with (not all of them have a p value column, and they have different nrs of columns). What would be the most elegant way to do this? – Tom Wenseleers Jul 07 '15 at 22:54
  • (this would be part of two functions I am writing table2doc and table2ppt to export R objects to tables in word or powerpoint, and so it is important it adequately deals with all possible xtable input objects) – Tom Wenseleers Jul 07 '15 at 22:55
  • you might write small functions to return the type of display and digits based on the column names of the fit object, a look-up table in essence. such as `get_display <- function(x) {key <- setNames(c('f','f','f','g'), c('Estimate','Std. Error','t value','Pr')); key[sapply(x, function(xx) grep(xx, names(key)))]} get_display(colnames(s$coefficients))` and I would use it like `get_display(colnames(s$coefficients))` which gives me the f,f,f,g above. And you can use a similar approach for setting the digits – rawr Jul 07 '15 at 23:20

1 Answers1

2

Define a helper function that will look for a signature of a p-value, which I am taking to be those colnames that have "Pr" in them:

xtable2 <- function(x, ...) {  sm <- x[['coefficients']]; ncol <- ncol(sm)
                             whch<- grep("Pr", colnames(sm))
                             digs <- rep(4, ncol+1); digs[whch+1] <- 2
                             disp <-rep("f", ncol+1); disp[whch+1] <- "g"
                  xtable(x, digits= digs, display=disp, ...) }

> tab <- as.FlexTable(xtable2(s)); tab

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • That's great - that was exactly what I was looking for - thanks millions !! – Tom Wenseleers Jul 08 '15 at 06:22
  • Works perfectly for the `lm` example, but for `aov` I get `fit = aov(weight ~ group); s=summary(fit); as.FlexTable(xtable2(s))` `Error in rep(4, ncol + 1) : invalid 'times' argument` - `x[['coefficients']]` there doesn't work - any thoughts how to fix that? – Tom Wenseleers Jul 08 '15 at 07:55
  • Ha with `asdf=function(x) { if(length(x) == 1) { as.data.frame(x[[1]]) } else { lapply(unlist(x, FALSE), as.data.frame) } }; sm = asdf(s)` it works – Tom Wenseleers Jul 08 '15 at 08:31
  • Ha or even better `sm=xtable(x)`also works and is more generic, working for any supported class of x! – Tom Wenseleers Jul 08 '15 at 10:00
  • The problem with aov is that it returns a list of objects that need to be process separate, which it appears you sorted out. `lm` and `glm` return a single list with `coefficient` leaves. – IRTFM Jul 08 '15 at 16:06
  • Thx again for your help and the clarification! Wrapped my functions to export stats objects (or R graphs) to MS Office in a small R package now at https://github.com/tomwenseleers/export2office – Tom Wenseleers Jul 09 '15 at 19:55