How to create horizontal tables in R (markdown)

Viewed 1266

I would like to present horizontal raw data tables for minimal datasets in R and R markdown. I havn't found a simple solution yet. Is there any table package that switches row and column by an easy default option?

---
title: "Horizontal Tables"
author: "Name"
date: "11 5 2021"
output: html_document
---

## Raw Data As Horizontal Table

```{r warning=FALSE, message=FALSE}
# raw data vertical
mydata <- data.frame(pass = (c(0,0,0,0,1,1,1,1)) ,
                     hours = c(2,1,3,2,4,7,4,8))
mydata

# (subset) of raw data horizontal
library(tidyverse)
glimpse(mydata)

# What package shows nice horizontal raw data tables?
library(gt)
gt(mydata)
```

The output should be transposed, i.e. rows and columns changed. glimpse() is showing horizontal values by default, but it lacks a nice table design.

enter image description here

1 Answers

Here is an idea using t() to transpose your table.

The code below uses knitr::kable() and kableExtra, you will find more information here to choose the style you prefer. If you want it to be very small: kable_styling(bootstrap_options = c("condensed"),full_width = F, position = "left")

---
title: "Horizontal Tables"
output: html_document
---

## Raw Data As Horizontal Table

```{r warning=FALSE, message=FALSE}
# raw data vertical
mydata <- data.frame(pass = (c(0,0,0,0,1,1,1,1)) ,
                     hours = c(2,1,3,2,4,7,4,8))
mydata

library(knitr)
library(kableExtra)
as.data.frame(t(mydata)) %>% kable(col.names = NULL) %>% kable_styling()
```

Output:

enter image description here

Related