How to go from R to nice tables in Microsoft Word
A step-by-step tutorial on exporting tables from R
![](/post/exporting-tables-from-r-to-microsoft-word/featured.png)
As scientists, we often have data or results in R that we want to export to Microsoft Word for the reports or publications that we’re writing.
In this tutorial I show you how to do just that. You can also watch this tutorial as a video if you want to follow along while I code:
The first step is to load up some data. We’re going to use the Orange
dataset that comes built into R, which describes the growth of orange trees:
# Load the data
data(Orange)
If we view the data, we can see the following columns: “Tree”, which contains an identifier for each tree that was measured; “age”, which contains the age (in days) of the tree at the time of measurement; and “circumference”, which is the circumference of the tree trunk, measured in millimeters.
head(Orange, 15)
## Tree age circumference
## 1 1 118 30
## 2 1 484 58
## 3 1 664 87
## 4 1 1004 115
## 5 1 1231 120
## 6 1 1372 142
## 7 1 1582 145
## 8 2 118 33
## 9 2 484 69
## 10 2 664 111
## 11 2 1004 156
## 12 2 1231 172
## 13 2 1372 203
## 14 2 1582 203
## 15 3 118 30
So in this dataset, there are five different trees, each of which have been measured at the same time points (age).
Let’s say we want to summarize this dataset to see how the different age groups compare in their growth. In the script below I’ve organized the data so that now we have a table called Orange_summ
, which shows the mean and standard deviation of the tree circumferences for each age group. (To run the code below, just make sure that the 'dplyr'
package is installed if not already):
# install.packages("dplyr")
library(dplyr)
Orange_summ <- group_by(Orange, "days"=age) %>%
summarize(mean_circ_mm = mean(circumference), sd_circ_mm = round(sd(circumference), 2))
Orange_summ
## # A tibble: 7 × 3
## days mean_circ_mm sd_circ_mm
## <dbl> <dbl> <dbl>
## 1 118 31 1.41
## 2 484 57.8 8.17
## 3 664 93.2 17.2
## 4 1004 134. 25.9
## 5 1231 146. 29.2
## 6 1372 173. 32.8
## 7 1582 176. 33.3
Great! Now we have a summary table that we can export to Word. First, we’re going to save our table as a ‘*.csv’ file.
write.csv(Orange_summ, "Orange_summ.csv", row.names = F)
What’s important to note here is that we set row.names
to False—doing this eliminates the row numbers in our .csv file, since we don’t need them.
Next, open the .csv file. You can see below that Microsoft Excel is the default software for opening .csv files, but we don’t want that. We’re going to open the file in TextEdit or a similar text editor by right-clicking on our file and choosing the appropriate app.
It should look something like this.
![a screenshot of the text editor showing the contents of the .csv file](/tablesinwordPhoto3.png)
After opening the .csv file in your text editor app, just copy and paste the text onto a blank Microsoft Word document.
In Word, highlight the text, and then go to Table » Convert » Convert Text to Table…
That will open a window where you should check that the number of columns is correct, and make sure you have chosen “Commas” in the “Separate text at” section. That’s because you saved the file as a .csv, or “comma-separated values” file.
![a screenshot from MS word showing the settings for converting text to table](/tablesinwordPhoto5.png)
Click “OK” and we have a table!
Next, use the “Find and Replace” function to clean up the table by going to Edit » Find » Replace. (The Mac keyboard shortcut for this is Shift + Command + H).
We want to get rid of all the double quotes in our table, so put double quotes “ in the top bar, and leave the bottom bar blank. Then click “Replace all”. Word should have found 6 replacements. This is definitely something that could have been fixed manually in this case since there are only 6 occurrences, but if your table contains a character or factor column, all the values in that column will end up having double quotes around them, so that’s where this trick comes in handy…
Looking good!
Now just rename the columns and reformat the table to make it nice and polished. Word has several border editing tools that allow you to change which borders are visible. I like to remove all borders first. Then, by putting your cursor in a table cell, you can go to Table Design » Border Painter, which lets you “paint” in whichever borders you do want to add.
And that’s it! You’ve just exported your first table from R into Microsoft Word.
Also be sure to check out R-bloggers for other great tutorials on learning R
The text editor step can be eliminated by exporting in tab-separated format, and then using Word's "Insert File" command. Followed, of course, by Word's Convert Text to Table.
For the tsv, I would use, for example,
write.table( matrix(1:12, ncol=4) , 'test.tsv', row.names=FALSE, quote=FALSE, sep='\t')
Setting quote=FALSE eliminates the quotes from the exported file, so there is no need to deal with them in Word.
Oh good point! This is an awesome tip. And saving even one more step will reduce the chance of error. Thanks so much for the additional advice.
you can also copy & paste to Excel and then again copy & paste to word.
I'm Selina, the Virtual Assistant for R for Ecology – copying & pasting is a good point! Thanks for the simple solution.
If we view the data, we can see the following columns: “Tree”, which contains an identifier for each tree that was measured; “age”, which contains the age (in days) of the tree at the time of measurement; and “circumference”, which is the circumference of the tree trunk, measured in millimeters.
I wrote about this about 10 years ago and one of my packages, htmlTable, is specifically geared towards dealing with tables that have Word compatibility (https://gforge.se/2020/07/news-in-htmltable-2-0/). I summarized my work-flow here: https://gforge.se/2014/07/fast-track-publishing-using-rmarkdown/ and although some minor changes have occurred the last decade, surprisingly little has changed and the package has 200 000+ downloads/month.
Thanks for a great description! For a way to minimise the need for copy/paste and getting publication-ready tables, check out this post! https://michaeldismorr.netlify.app/post/publication-ready-tables-with-flextable-and-own-theme-in-r/
Thanks for sharing! I really do appreciate all the alternative solutions and methods for doing this that are being shared here. I may follow up a future post with the
flextable
methodIf the reader is interested in a easy way to export tables from R directly to Word without using RMarkdown, one way might be the package "apaTables" (https://cran.r-project.org/web/packages/apaTables/ ). It can create common APA style tables (e.g., descriptives, regression results, etc). I use it quite often. Cheers, Manuel
That's very cool! Thanks for sharing this, Manuel!
You can also use any of the packages that allows rendering from R Markdown into Word, such as the huxtable package (for more, https://www.entrepreneur.com/article/390345)
Good to know about that when using R Markdown! Thanks for sharing.
This is a neat idea, I didn't know Word has a text to take feature.
Nowadays I just use RMarkdown (or more precisely, regular R scripts that I render into RMarkdown and then into Word) together with the Pander package.
I basically write the whole paper in the commented sections or my R script with all tables and figures and compile it to a word document in one shot.
That's interesting. I'm actually planning a post just about RMarkdown soon. Thanks for sharing!