5

In order to include tables in my shared word processing documents (I'm stuck using MS Word) I currently have to recreate them in the Word document itself.

Is there a relatively "easy" way (e.g., not needing to learn LaTeX) to output my data from R into an APA-style formatted table (e.g., as a PDF) that I could insert instead?

Thanks!

Concept Delta
  • 187
  • 2
  • 10
  • @James. Thank you James. He also pointed me to this answer by agstudy using the Kmisc package: http://stackoverflow.com/questions/14547069/how-to-write-from-r-to-the-clipboard-on-a-mac – Concept Delta Oct 18 '13 at 15:27

4 Answers4

1

You can use write.table to send it to the clipboard, and then copy the text into Word. You need to remember to set sep="\t" and col.names=NA For example:

write.table(xtabs(hp~cyl+am,mtcars),"clipboard",sep="\t",col.names=NA)

In Word, highlight the copied text and then click on Insert Table... and you should have all the data in a table now. You can apply a table style to pretty it up, and create you own APA one to get everything to your liking.

Text strings (and especially row and column headings) will likely have quotation marks around them, so you might have some work to do removing those.

James
  • 65,548
  • 14
  • 155
  • 193
1

As of 2022, one can use the rempsyc package to export an R dataframe to an APA-formatted Word table (Times New Roman size 12, only some horizontal lines, double-spaced, right number of decimals, 95% confidence intervals, special characters, etc.).

Reprex

You can specify table title and footnote:

library(rempsyc)
#> Tutorials: https://remi-theriault.com/tutorials
#> Bug report, support, special request: https://github.com/rempsyc/rempsyc/issues
#> 
#> Suggested APA citation: Thériault, R. (2022). rempsyc: Convenience functions for psychology (R package version 0.0.5.5) [Computer software]. https://rempsyc.remi-theriault.com
nice_table(mtcars[1:3, ], 
           title = c("Table 1", "Motor Trend Car Road Tests"),
           footnote = c("The data was extracted from the 1974 Motor Trend US magazine.",
                        "* p < .05, ** p < .01, *** p < .001"))

You can apply it to statistical tables made by yourself, or by the broom or report packages, and correct formatting will be applied.

nice_t_test(data = mtcars,
            response = c("mpg", "disp", "drat"),
            group = "am",
            warning = FALSE) -> stats.table
stats.table
#>   Dependent Variable         t       df            p         d   CI_lower
#> 1                mpg -3.767123 18.33225 1.373638e-03 -1.477947 -2.2659731
#> 2               disp  4.197727 29.25845 2.300413e-04  1.445221  0.6417834
#> 3               drat -5.646088 27.19780 5.266742e-06 -2.003084 -2.8592770
#>     CI_upper
#> 1 -0.6705686
#> 2  2.2295592
#> 3 -1.1245498
nice_table(stats.table)


library(report)
model <- lm(mpg ~ cyl + wt * hp, mtcars)
(stats.table <- as.data.frame(report(model)))
#> Parameter   | Coefficient |          95% CI | t(27) |      p | Std. Coef. | Std. Coef. 95% CI |    Fit
#> ------------------------------------------------------------------------------------------------------
#> (Intercept) |       49.49 | [ 41.97, 57.01] | 13.51 | < .001 |      -0.18 |    [-0.36, -0.01] |       
#> cyl         |       -0.37 | [ -1.41,  0.68] | -0.72 | 0.479  |      -0.11 |    [-0.42,  0.20] |       
#> wt          |       -7.63 | [-10.75, -4.51] | -5.01 | < .001 |      -0.62 |    [-0.85, -0.40] |       
#> hp          |       -0.11 | [ -0.17, -0.05] | -3.64 | 0.001  |      -0.29 |    [-0.53, -0.04] |       
#> wt * hp     |        0.03 | [  0.01,  0.04] |  3.23 | 0.003  |       0.29 |    [ 0.11,  0.47] |       
#>             |             |                 |       |        |            |                   |       
#> AIC         |             |                 |       |        |            |                   | 147.01
#> BIC         |             |                 |       |        |            |                   | 155.80
#> R2          |             |                 |       |        |            |                   |   0.89
#> R2 (adj.)   |             |                 |       |        |            |                   |   0.87
#> Sigma       |             |                 |       |        |            |                   |   2.17

To save to Word, simply use:

flextable::save_as_docx(stats.table, path = "D:/R treasures/nice_tablehere.docx")

Created on 2022-08-03 by the reprex package (v2.0.1)

Tutorial

The full tutorial is available here: https://rempsyc.remi-theriault.com/articles/table.html

Installation

Edit: The package is now on CRAN:

install.packages("rempsyc")

Disclaimer: If it wasn't clear from the name of the package, I am the author of this package :-)

rempsyc
  • 785
  • 5
  • 24
0

I had a similar question and found the package schoRsch to be helpful. You will still need to copy and paste the output to Word, but this package will format your data and has a wrapper function for the copy/paste step. Also, if you are doing ANOVA (as I was in this case), you'll have to use the ez package to do the actual analysis.

Patrick Williams
  • 694
  • 8
  • 22
0

Use the clipr package:

install.packages("clipr")
clipr::write_clip(mtcars)
Justapigeon
  • 560
  • 1
  • 8
  • 22