MBY
MBY

Reputation: 123

Research-style frequency table formatting with R Markdown

I am running frequencies for a peer-reviewed research paper and need to combine output into a table. An example of the format can be found here under "Results".

Essentially, it follows a style of heading/variable, with categories indented below, and n(%), either in one column or two.

I've tried to figure out a way to combine summarytools::freq() and knittr::kable() to get tables styled this way. One thing that I've tried is combining multiple freq() %>% as.data.frame() statements with bind_rows(). But that output is not very pretty.

I'm trying to avoid having to do a lot of cutting and pasting in Word because there were be a lot of tables, and I would like to get as close as possible to the desired format in my output. Any pointers?

Thank you!

Upvotes: 0

Views: 1424

Answers (2)

Dominic Comtois
Dominic Comtois

Reputation: 10411

The example you refer to uses cross-tabulation (joint frequencies for two categorical variables). So summarytools::freq() is probably not what you're looking for here, even though technically it can work if you use it with stby(). summarytools::ctable() would bring you a step closer, but there are other packages that might be better suited for this purpose, I'm thinking maybe tableone.

Upvotes: 2

Virgil Ion
Virgil Ion

Reputation: 131

You can use the xtable package in Rmarkdown to generate literature/research tables. This is the rmarkdown setup I used:

---
title: "Summary_results_as_research_table_SO"
output:
  pdf_document: default
  html_document:
    df_print: paged
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r , results="asis"}
library(xtable)
xtable(summary(iris))

```

Result: enter image description here

Upvotes: 0

Related Questions