Reputation: 197
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!
Upvotes: 6
Views: 10311
Reputation: 1018
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.).
You can specify table title and footnote:
library(rempsyc)
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.
stats.table <- nice_t_test(data = mtcars,
response = c("mpg", "disp", "drat"),
group = "am",
warning = FALSE)
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
flextable::save_as_docx(stats.table, path = "nice_table.docx")
Created on 2022-08-03 by the reprex package (v2.0.1)
The full tutorial is available here: https://rempsyc.remi-theriault.com/articles/table.html
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 :-)
Upvotes: 2
Reputation: 590
Use the clipr package:
install.packages("clipr")
clipr::write_clip(mtcars)
Upvotes: 0
Reputation: 704
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.
Upvotes: 0
Reputation: 66874
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.
Upvotes: 1