vtable {vtable} | R Documentation |
Variable Table Function
Description
This function will output a descriptive variable table either to the console or as an HTML file that can be viewed continuously while working with data. vt()
is the same thing but requires fewer key presses to type.
Usage
vtable(
data,
out = NA,
file = NA,
labels = NA,
class = TRUE,
values = TRUE,
missing = FALSE,
index = FALSE,
factor.limit = 5,
char.values = FALSE,
data.title = NA,
desc = NA,
note = NA,
note.align = "l",
anchor = NA,
col.width = NA,
col.align = NA,
align = NA,
fit.page = NA,
summ = NA,
lush = FALSE,
opts = list()
)
vt(
data,
out = NA,
file = NA,
labels = NA,
class = TRUE,
values = TRUE,
missing = FALSE,
index = FALSE,
factor.limit = 5,
char.values = FALSE,
data.title = NA,
desc = NA,
note = NA,
note.align = "l",
anchor = NA,
col.width = NA,
col.align = NA,
align = NA,
fit.page = NA,
summ = NA,
lush = FALSE,
opts = list()
)
Arguments
data |
Data set; accepts any format with column names. If variable labels are set with the haven package, |
out |
Determines where the completed table is sent. Set to |
file |
Saves the completed variable table file to HTML or .tex with this filepath. May be combined with any value of |
labels |
Variable labels. labels will accept three formats: (1) A vector of the same length as the number of variables in the data, in the same order as the variables in the data set, (2) A matrix or data frame with two columns and more than one row, where the first column contains variable names (in any order) and the second contains labels, or (3) A matrix or data frame where the column names (in any order) contain variable names and the first row contains labels. Setting the labels parameter will override any variable labels already in the data. Set to |
class |
Set to |
values |
Set to |
missing |
Set to |
index |
Set to |
factor.limit |
Sets maximum number of factors that will be included if |
char.values |
Set to |
data.title |
Character variable with the title of the dataset. |
desc |
Character variable offering a brief description of the dataset itself. This will by default include information on the number of observations and the number of columns. To remove this, set |
note |
Table note to go after the last row of the table. |
note.align |
Set the alignment for the multi-column table note. Usually "l", but if you have a long note in LaTeX you might want to set it with "p" |
anchor |
Character variable to be used to set an anchor link in HTML tables, or a label tag in LaTeX. |
col.width |
Vector of page-width percentages, on 0-100 scale, overriding default column widths in HTML table. Must have a number of elements equal to the number of columns in the resulting table. |
col.align |
For HTML output, a character vector indicating the HTML |
align |
For LaTeX output, string indicating the alignment of each column. Use standard LaTeX syntax (i.e. |
fit.page |
For LaTeX output, uses a resizebox to force the table to a certain width. Set to |
summ |
Character vector of summary statistics to include for numeric and logical variables, in the form |
lush |
Set to |
opts |
The same |
Details
Outputting the variable table as a help file will make it easy to search through variable names or labels, or to refer to information about the variables easily.
This function is in a similar spirit to promptData()
, but focuses on variable documentation rather than dataset documentation.
If you would like to include a vtable
in an RMarkdown document, it should just work! If you leave out
blank, it will default to a nicely-formatted knitr::kable()
, although this will drop some formatting elements like multi-column cells (or do out="kable"
to get an unformatted kable
that you can format yourself). If you prefer the vtable
package formatting, then use out="latex"
if outputting to LaTeX or out="htmlreturn"
for HTML, both with results="asis"
in the code chunk. Alternately, in HTML, you can use the file
option to write to file and use a <iframe>
to include it.
Examples
if(interactive()){
df <- data.frame(var1 = 1:4,var2=5:8,var3=c('A','B','C','D'),
var4=as.factor(c('A','B','C','C')),var5=c(TRUE,TRUE,FALSE,FALSE))
#Demonstrating different options:
vtable(df,labels=c('Number 1','Number 2','Some Letters',
'Some Labels','You Good?'))
vtable(subset(df,select=c(1,2,5)),
labels=c('Number 1','Number 2','You Good?'),class=FALSE,values=FALSE)
vtable(subset(df,select=c('var1','var4')),
labels=c('Number 1','Some Labels'),
factor.limit=1,col.width=c(10,10,40,35))
#Different methods of applying variable labels:
labelsmethod2 <- data.frame(var1='Number 1',var2='Number 2',
var3='Some Letters',var4='Some Labels',var5='You Good?')
vtable(df,labels=labelsmethod2)
labelsmethod3 <- data.frame(a =c("var1","var2","var3","var4","var5"),
b=c('Number 1','Number 2','Some Letters','Some Labels','You Good?'))
vtable(df,labels=labelsmethod3)
#Using value labels and pre-labeled data:
library(sjlabelled)
df <- set_label(df,c('Number 1','Number 2','Some Letters',
'Some Labels','You Good?'))
df$var1 <- set_labels(df$var1,labels=c('A little','Some more',
'Even more','A lot'))
vtable(df)
#efc is data with embedded variable and value labels from the sjlabelled package
library(sjlabelled)
data(efc)
vtable(efc)
#Displaying the values of a character vector
data(USJudgeRatings)
USJudgeRatings$Judge <- row.names(USJudgeRatings)
vtable(USJudgeRatings,char.values=c('Judge'))
#Adding summary statistics for variable mean and proportion of data that is missing.
vtable(efc,summ=c('mean(x)','propNA(x)'))
}