Summary outputs as nice tables?


Is there a good way to save the output of a statistical summary to file? Something like a comma-delimited text file that can be opened in a spreadsheet program. For example:

x <- rnorm(10)
y <- rnorm(10)
mod <- lm(y ~ x)


lm(formula = y ~ x)

    Min      1Q  Median      3Q     Max 
-1.2249 -0.9493 -0.3325  0.8500  1.6135 

            Estimate Std. Error t value Pr(>|t|)
(Intercept)  -0.2030     0.4336  -0.468    0.652
x             0.3002     0.4558   0.659    0.529

Residual standard error: 1.15 on 8 degrees of freedom
Multiple R-squared:  0.05143,	Adjusted R-squared:  -0.06714 
F-statistic: 0.4338 on 1 and 8 DF,  p-value: 0.5286

In particular, I’d like to save the Coefficients as a table to put in a manuscript. (I’m sick of copying and pasting this stuff.)


Hi Josh,

To construct a table of statistics, I would first use str() to determine the names and components of the object generated by the statistical test:


I would then construct an empty dataframe that includes columns for all the output that I want to include my table. In the example below, I have included columns for the estimates and standard errors for the intercept and slope of the model, the r^2 of the model, and the p value of the model.

I would then populate the dataframe as I run the statistics by pulling out the necessary bits, as in the example below. Finally, I would use write.csv() to export the dataframe to a text file.

Hope that helps.



#empty dataframe used to construct table
table1 <-data.frame(model.num=1,intercept=1,,slope=1,,r.squared=1,p.value=1)[-1,] 

#fit ten models and put output in table
for(i in 1:10) {

#generate random data
x <- rnorm(10)
y <- rnorm(10)
mod <- lm(y ~ x)
#add summary statistics to dataframe table
        #I used str(summary(mod)) to determine how to get the bits I needed
table1[i,] <- c(i,
                     summary(mod)[['coefficients']]['(Intercept)','Std. Error'],
                     summary(mod)[['coefficients']]['x','Std. Error'],

#writes dataframe to your working directory as text file


Thanks, Drew. This is super helpful.

What we need is a function for mapping/coercing summary(mod)$coefficients (or any R text output) directly into a data frame. So you could do something like this:

write.csv(make_pretty(summary(mod)$coefficients), file="table1.csv")

Such a “make_pretty” function would save a lot of messing about…


Turns out data.frame actually does it!

write.csv(data.frame(summary(mod)$coefficients), file="table1.csv")

Had no idea. Awesome.

But it won’t work on unbalanced outputs, like the complete summary, e.g.:

write.csv(data.frame(summary(mod)), file="table1.csv")

So, looks like as long as the output is table-like, data.frame does the trick.


Great tips - thanks you two.


Really helpful string - thanks, guys.



A bit late to the discussion but an alternative approach to consider is using broom.
example below.


library(broom) # tidies model objects

# Data set  with variation - (2 models)
x <- rnorm(10)
y <- rnorm(10)
z <- rnorm(10)

mod.A <- lm(y ~ x)
mod.B <- lm(y ~ x+z)

# Messy model output

# Tidy approach
# Use broom to easier extract model components
# Three functions to learn - tidy, glance & augment
# can use pipe %>% - pronounced 'then'

# Model co-efficents

# Alternatively write with magritter pipe
mod.A %>% tidy

# Model statistics
mod.A %>% glance

# Model data
# same as head(augment(mod.A),3)
mod.A %>% augment() %>% head(3)

# Use these to extract and save into data frame objects.
# Model names
Mod.Compare <- data.frame(Mod.Name= c("mod.A", "mod.B"))
# Bind with model stats extracted with glance
Mod.Compare <- cbind(Mod.Compare,
# Compare models

# This Mod.Compare object form also makes it easier to publish in RMarkdown tables or write to a csv or Excel file. 

# btw also helps with quick eda
mod.A %>% augment() %>% ggplot(aes(x=.fitted,y=.resid ,colour=z)) + geom_point()


Incredible! Thanks so much for demonstrating that, Justin. Getting text from R into useful formats has been a pet peeve of mine about R for years. I used to copy and paste values from the stats tables! (ages ago, of course… :slight_smile: )



Glad the codes of some interest.


Second part - following on from prior post…

Use the “Model.Compare”" object for publishing in RMarkdown.

There are a few packages you can use to make tables.

Example code with with Kintr and Huxtable in Rmarkdown below.


#simple quick table

# Alternately use huxtable

# Make a hux object
Mod.Compare.Hux <- as_huxtable(Mod.Compare)

# simple output

# Customise output with pipes.
Mod.Compare.Hux %>%
  set_caption('Model Comparison') %>% 
  add_colnames() %>% 
  set_all_borders(1) %>% 
  theme_striped(stripe = 'bisque1',header_col = FALSE,  header_row = FALSE) %>% # will create an alterante stripe
    set_background_color( 1,1:12,'lightblue') %>% 
  set_background_color( 2,10,'lightgreen') %>%  #colour