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:
vtable
, orvt
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.sumtable
orst
for short, provides a table of summary statistics. It is very similar in spirit to the summary statistics function ofstargazer::stargazer()
except that it acceptstibble
s, handles factor variables, and makes by-group statistics and group tests easy.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
sumtable
s 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)