2

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

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
alawipt
  • 33
  • 4
  • 5
    Welcome to Stack Overflow! It might be easier to help if you could [edit] your question to include some more detail on what you mean by `export it as a table`. Are you needing a Word document with a table, LaTeX code for a table, etc.? – duckmayr Jul 25 '20 at 21:48
  • Why don't you export your data to csv (using `write.table`) and then format it on LibreOffice Calc or Excel? – Rodrigo Jul 26 '20 at 18:09

2 Answers2

0

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
mustafaakben
  • 501
  • 2
  • 5
0

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.

dash2
  • 2,024
  • 6
  • 15