SkillAgentSearch skills...

Vtable

An R package for creating Variable Documentation Files

Install / Use

/learn @NickCH-K/Vtable
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<!-- README.md is generated from README.Rmd. Please edit that file -->

vtable

<!-- badges: start -->

CRAN
checks CRAN
status

<!-- badges: end -->

The vtable package is designed to help you quickly and efficiently look at and document your data.

There are three main functions in vtable:

  1. vtable, or vt for short, shows you information about the variables in your data set, including variable labels, in a way that is easy to use “find in page” to search through. It was designed to be similar to Stata’s “Variables” panel.
  2. sumtable or st for short, provides a table of summary statistics. It is very similar in spirit to the summary statistics function of stargazer::stargazer() except that it accepts tibbles, handles factor variables, and makes by-group statistics and group tests easy.
  3. labeltable provides a table of value labels, either for variables labelled with sjlabelled or haven or similar, or for when you want to see how the values of one column line up with the values of another.

All three of these functions are built with the intent of being fast. Not so much fast to run, but fast to use. The defaults are intended to be good defaults, and the output by default prints to the Viewer tab (in RStudio) or the browser (outside RStudio) so you can see it immediately, and continue to look at it as you work on your data.

You could almost certainly build your own highly-customized version of vtable, But why do that when you can just do vt(df) and see the information you need to see? And there are eight million packages that make summary statistics tables to your exact specifications if you tweak them. But there’s a good chance that st(df) does what you want. If you want something real out there, that’s when you can break out the big guns.

All three main vtable functions can produce HTML, LaTeX, data.frame, CSV, or knitr::kable() output.

Installation

You can install vtable from CRAN. Note that the documentation on this site refers to the development version, and so may not work perfectly for the CRAN version. But the two will usually be the same.:

install.packages("vtable")

Development version

The development version can be installed from GitHub:

# install.packages("remotes")
remotes::install_github("NickCH-K/vtable")

vtable Example

I’ll just do a brief example here, using the iris we all know and love. Output will be to kable since this is an RMarkdown document.

data(iris)

# Basic vtable
vt(iris)
<table class="table" style="margin-left: auto; margin-right: auto;"> <caption> iris </caption> <thead> <tr> <th style="text-align:left;"> Name </th> <th style="text-align:left;"> Class </th> <th style="text-align:left;"> Values </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Sepal.Length </td> <td style="text-align:left;"> numeric </td> <td style="text-align:left;"> Num: 4.3 to 7.9 </td> </tr> <tr> <td style="text-align:left;"> Sepal.Width </td> <td style="text-align:left;"> numeric </td> <td style="text-align:left;"> Num: 2 to 4.4 </td> </tr> <tr> <td style="text-align:left;"> Petal.Length </td> <td style="text-align:left;"> numeric </td> <td style="text-align:left;"> Num: 1 to 6.9 </td> </tr> <tr> <td style="text-align:left;"> Petal.Width </td> <td style="text-align:left;"> numeric </td> <td style="text-align:left;"> Num: 0.1 to 2.5 </td> </tr> <tr> <td style="text-align:left;"> Species </td> <td style="text-align:left;"> factor </td> <td style="text-align:left;"> ‘setosa’ ‘versicolor’ ‘virginica’ </td> </tr> </tbody> </table>

There are plenty of options if we want to go nuts, but let’s keep it simple and just ask for a little more with lush

vt(iris, lush = TRUE)
<table class="table" style="margin-left: auto; margin-right: auto;"> <caption> iris </caption> <thead> <tr> <th style="text-align:left;"> Name </th> <th style="text-align:left;"> Class </th> <th style="text-align:left;"> Values </th> <th style="text-align:left;"> Missing </th> <th style="text-align:left;"> Summary </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Sepal.Length </td> <td style="text-align:left;"> numeric </td> <td style="text-align:left;"> Num: 4.3 to 7.9 </td> <td style="text-align:left;"> 0 </td> <td style="text-align:left;"> mean: 5.843, sd: 0.828, nuniq: 35 </td> </tr> <tr> <td style="text-align:left;"> Sepal.Width </td> <td style="text-align:left;"> numeric </td> <td style="text-align:left;"> Num: 2 to 4.4 </td> <td style="text-align:left;"> 0 </td> <td style="text-align:left;"> mean: 3.057, sd: 0.436, nuniq: 23 </td> </tr> <tr> <td style="text-align:left;"> Petal.Length </td> <td style="text-align:left;"> numeric </td> <td style="text-align:left;"> Num: 1 to 6.9 </td> <td style="text-align:left;"> 0 </td> <td style="text-align:left;"> mean: 3.758, sd: 1.765, nuniq: 43 </td> </tr> <tr> <td style="text-align:left;"> Petal.Width </td> <td style="text-align:left;"> numeric </td> <td style="text-align:left;"> Num: 0.1 to 2.5 </td> <td style="text-align:left;"> 0 </td> <td style="text-align:left;"> mean: 1.199, sd: 0.762, nuniq: 22 </td> </tr> <tr> <td style="text-align:left;"> Species </td> <td style="text-align:left;"> factor </td> <td style="text-align:left;"> ‘setosa’ ‘versicolor’ ‘virginica’ </td> <td style="text-align:left;"> 0 </td> <td style="text-align:left;"> nuniq: 3 </td> </tr> </tbody> </table>

sumtable Example

Let’s stick with iris!

# Basic summary stats
st(iris)
<table class="table" style="margin-left: auto; margin-right: auto;"> <caption> Summary Statistics </caption> <thead> <tr> <th style="text-align:left;"> Variable </th> <th style="text-align:left;"> N </th> <th style="text-align:left;"> Mean </th> <th style="text-align:left;"> Std. Dev. </th> <th style="text-align:left;"> Min </th> <th style="text-align:left;"> Pctl. 25 </th> <th style="text-align:left;"> Pctl. 75 </th> <th style="text-align:left;"> Max </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Sepal.Length </td> <td style="text-align:left;"> 150 </td> <td style="text-align:left;"> 5.843 </td> <td style="text-align:left;"> 0.828 </td> <td style="text-align:left;"> 4.3 </td> <td style="text-align:left;"> 5.1 </td> <td style="text-align:left;"> 6.4 </td> <td style="text-align:left;"> 7.9 </td> </tr> <tr> <td style="text-align:left;"> Sepal.Width </td> <td style="text-align:left;"> 150 </td> <td style="text-align:left;"> 3.057 </td> <td style="text-align:left;"> 0.436 </td> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> 2.8 </td> <td style="text-align:left;"> 3.3 </td> <td style="text-align:left;"> 4.4 </td> </tr> <tr> <td style="text-align:left;"> Petal.Length </td> <td style="text-align:left;"> 150 </td> <td style="text-align:left;"> 3.758 </td> <td style="text-align:left;"> 1.765 </td> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> 1.6 </td> <td style="text-align:left;"> 5.1 </td> <td style="text-align:left;"> 6.9 </td> </tr> <tr> <td style="text-align:left;"> Petal.Width </td> <td style="text-align:left;"> 150 </td> <td style="text-align:left;"> 1.199 </td> <td style="text-align:left;"> 0.762 </td> <td style="text-align:left;"> 0.1 </td> <td style="text-align:left;"> 0.3 </td> <td style="text-align:left;"> 1.8 </td> <td style="text-align:left;"> 2.5 </td> </tr> <tr> <td style="text-align:left;"> Species </td> <td style="text-align:left;"> 150 </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> </tr> <tr> <td style="text-align:left;"> … setosa </td> <td style="text-align:left;"> 50 </td> <td style="text-align:left;"> 33.3% </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> </tr> <tr> <td style="text-align:left;"> … versicolor </td> <td style="text-align:left;"> 50 </td> <td style="text-align:left;"> 33.3% </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> </tr> <tr> <td style="text-align:left;"> … virginica </td> <td style="text-align:left;"> 50 </td> <td style="text-align:left;"> 33.3% </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> <td style="text-align:left;"> </td> </tr> </tbody> </table>

Note that sumtable allows for much more customization than vtable since there’s a heightened chance you want it for a paper or something. But I’ll leave that to the more detailed documentation. For now just note it does by-group stats, either in “group.long” format (multiple sumtables stacked on top of each other), or by default, in columns, with an option to add a group test.

Grouped sumtables look a little nicer in formats that suport multi-column cells like HTML and LaTeX.

These tables include multi-column cells, which are not supported in the kable output, but are supported by vtable’s dftoHTML and dftoLaTeX functions. They look nicer in the HTML or LaTeX output.

st(iris, 
   gro

Related Skills

View on GitHub
GitHub Stars40
CategoryDevelopment
Updated11mo ago
Forks6

Languages

R

Security Score

67/100

Audited on May 1, 2025

No findings