Learn R Programming

vtable

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)

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)

sumtable Example

Let’s stick with iris!

# Basic summary stats
st(iris)

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, 
   group = 'Species', 
   group.test = TRUE)

Species

setosa

versicolor

virginica

labeltable Example

For this we’ll need labeled values.

data(efc, package = 'sjlabelled')

# Now shoot - how was gender coded?
labeltable(efc$e16sex)

labeltable can also be used to see, for values of one variable, what values are present of other variables. This is intended for use if one variable is a recode, simplification, or lost-labels version of another, but hey, go nuts.

labeltable(efc$e15relat,efc$e16sex,efc$e42dep)

Copy Link

Version

Install

install.packages('vtable')

Monthly Downloads

3,884

Version

1.4.8

License

MIT + file LICENSE

Maintainer

Nick Huntington-Klein

Last Published

December 21st, 2024

Functions in vtable (1.4.8)

pctile

Returns a vector of 100 percentiles
countNA

Number of missing values in a vector
labeltable

Label Table Function
nuniq

Number of unique values in a vector
dftoHTML

Data Frame to HTML Function
is.round

Checks if information is lost by rounding
notNA

Number of nonmissing values in a vector
dftoLaTeX

Data Frame to LaTeX Function
formatfunc

Function-returning wrapper for format
independence.test

Group-Independence Test Function
sumtable

Summary Table Function
vtable

Variable Table Function
propNA

Proportion or number of missing values in a vector
weighted.sd

Weighted standard deviation