How to create tables in R

Viewed 763

I am trying to create and export a table for publication (picture attached).

I have created a table using the code below, but I could not export it as a table.

Can anyone help, please

library(tidyverse)
library(gapminder)
data(gapminder)

median_gdp <- median(gapminder$gdpPercap)
gapminder %>%
select(-country) %>%
mutate(gdpPercap = ifelse(gdpPercap > median_gdp, "high", "low")) %>%
mutate(gdpPercap = factor(gdpPercap)) %>%
mutate(pop = pop / 1000000) -> gapminder

gapminder <- lapply(gapminder, function(x) x[sample(c(TRUE, NA),
                                                prob = c(0.9, 0.1),
                                                size = length(x),
                                                replace = TRUE
 )])

 library(arsenal)
 table_one <- tableby(continent ~ ., data = gapminder)
 summary(table_one, title = "Gapminder Data", text=TRUE)

image

2 Answers

If you want to write a table to Microsoft Word, you can use the following code from arsenal package.

write2word(table_one, "table.doc",
           keep.md = TRUE,
           quiet = TRUE,
           title = "Your title") 

You can also write a table to pdf and HTML by using the arsenal package. For the details, see

?write2specific

Weirdly, there doesn't seem to be a general question on this topic, though see Create a PDF table for PDFs.

Modern packages to print tables in output formats, including PDF, HTML and Word, include gt, huxtable, flextable and kableExtra.

Packages to create tables of summary statistics include skimr, summarytools and qwraps2. Some of these also have built-in output to different formats.

There are many other packages out there.

Related