This function output a descriptive table listing, for each value of a given variable, either the label of that value, or all values of another variable associated with that value. The table is output either to the console or as an HTML file that can be viewed continuously while working with data.

labeltable(
  var,
  ...,
  out = NA,
  count = FALSE,
  percent = FALSE,
  file = NA,
  desc = NA,
  note = NA,
  note.align = NA,
  anchor = NA
)

Arguments

var

A vector. Label table will show, for each of the values of this variable, its label (if labels can be found with sjlabelled::get_labels()), or the values in the ... variables.

...

As described above. If specified, will show the values of these variables, instead of the labels of var, even if labels can be found.

out

Determines where the completed table is sent. Set to "browser" to open HTML file in browser using browseURL(), "viewer" to open in RStudio viewer using viewer(), if available. Use "htmlreturn" to return the HTML code to R, "return" to return the completed variable table to R in data frame form, or "kable" to return it in knitr::kable() form. Combine out = "csv" with file to write to CSV (dropping most formatting). Additional options include "latex" for a LaTeX table or "latexpage" for a full buildable LaTeX page. Defaults to "viewer" if RStudio is running, "browser" if it isn't, or a "kable" passed through kableExtra::kable_styling() defaults if it's an RMarkdown document being built with knitr.

count

Set to TRUE to also report the number of observations for each value of var in the data.

percent

Set to TRUE to also report the percentage of non-missing observation for each value of var in the data.

file

Saves the completed variable table file to HTML with this filepath. May be combined with any value of out, although note that out = "return" and out = "kable" will still save the standard labeltable HTML file as with out = "viewer" or out = "browser"..

desc

Description of variable (or labeling system) to be included with the table.

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.

Details

Outputting the label table as a help file will make it easy to search through value labels, or to see the correspondence between the values of one variable and the values of another.

Labels that are not in the data will also be reported in the table.

Examples

#> 
#>                     <html style="font-family:Helvetica,Arial,Sans">
#>                     <head><title>e15relat Label Table</title><style type = "text/css">
#>                     p {
#>                     font-size:smaller;
#>                     }
#>                     table {
#>                     border: 0px;
#>                     border-collapse:collapse;
#>                     font-size:smaller;
#>                     table-layout:fixed;
#>                     margin-left:0%;
#>                     margin-right:auto;
#>                     }
#>                     .headtab {
#>                     width: 100%;
#>                     margin-left:auto;
#>                     margin-right:auto;
#>                     }
#>                     th {
#>                     background-color: #FFFFFF;
#>                     font-weight:bold;
#>                     text-align:left;
#>                     }
#>                     table tr:nth-child(odd) td {
#>                     background-color: #FFFFFF;
#>                     padding:4px;
#>                     word-wrap: break-word;
#>                     word-break:break-all;
#>                     }
#>                     table tr:nth-child(even) td {
#>                     background-color: #D3D3D3;
#>                     padding:4px;
#>                     word-wrap: break-word;
#>                     word-break:break-all;
#>                     }</style></head><body> <table class="headtab"> <tr><td style="text-align:left">labeltable {vtable}</td> <td style="text-align:right">Variable Documentation: Label Table</td></tr></table> <h1> e15relat </h1><h3>Label Table</h3><table><tr><th style = "width:50%; text-align:left">e15relat</th><th style = "width:50%; text-align:left">Label</th></tr>
#> <tr><td style = "width:50%; text-align:left">1</td><td style = "width:50%; text-align:left">spouse/partner</td></tr>
#> <tr><td style = "width:50%; text-align:left">2</td><td style = "width:50%; text-align:left">child</td></tr>
#> <tr><td style = "width:50%; text-align:left">3</td><td style = "width:50%; text-align:left">sibling</td></tr>
#> <tr><td style = "width:50%; text-align:left">4</td><td style = "width:50%; text-align:left">daughter or son -in-law</td></tr>
#> <tr><td style = "width:50%; text-align:left">5</td><td style = "width:50%; text-align:left">ancle/aunt</td></tr>
#> <tr><td style = "width:50%; text-align:left">6</td><td style = "width:50%; text-align:left">nephew/niece</td></tr>
#> <tr><td style = "width:50%; text-align:left">7</td><td style = "width:50%; text-align:left">cousin</td></tr>
#> <tr><td style = "width:50%; text-align:left">8</td><td style = "width:50%; text-align:left">other, specify</td></tr>
#> </table></body></html>
#>                     <html style="font-family:Helvetica,Arial,Sans">
#>                     <head><title>e15relat Label Table</title><style type = "text/css">
#>                     p {
#>                     font-size:smaller;
#>                     }
#>                     table {
#>                     border: 0px;
#>                     border-collapse:collapse;
#>                     font-size:smaller;
#>                     table-layout:fixed;
#>                     margin-left:0%;
#>                     margin-right:auto;
#>                     }
#>                     .headtab {
#>                     width: 100%;
#>                     margin-left:auto;
#>                     margin-right:auto;
#>                     }
#>                     th {
#>                     background-color: #FFFFFF;
#>                     font-weight:bold;
#>                     text-align:left;
#>                     }
#>                     table tr:nth-child(odd) td {
#>                     background-color: #FFFFFF;
#>                     padding:4px;
#>                     word-wrap: break-word;
#>                     word-break:break-all;
#>                     }
#>                     table tr:nth-child(even) td {
#>                     background-color: #D3D3D3;
#>                     padding:4px;
#>                     word-wrap: break-word;
#>                     word-break:break-all;
#>                     }</style></head><body> <table class="headtab"> <tr><td style="text-align:left">labeltable {vtable}</td> <td style="text-align:right">Variable Documentation: Label Table</td></tr></table> <h1> e15relat </h1><h3>Label Table</h3><table><tr><th style = "width:33%; text-align:left">e15relat</th><th style = "width:33%; text-align:left">e16sex</th><th style = "width:33%; text-align:left">e42dep</th></tr>
#> <tr><td style = "width:33%; text-align:left">1</td><td style = "width:33%; text-align:left">2, 1</td><td style = "width:33%; text-align:left">3, 4, 1, 2, NA</td></tr>
#> <tr><td style = "width:33%; text-align:left">2</td><td style = "width:33%; text-align:left">2, 1, NA</td><td style = "width:33%; text-align:left">3, 4, 2, 1</td></tr>
#> <tr><td style = "width:33%; text-align:left">3</td><td style = "width:33%; text-align:left">1, 2</td><td style = "width:33%; text-align:left">3, 2, 1, 4</td></tr>
#> <tr><td style = "width:33%; text-align:left">4</td><td style = "width:33%; text-align:left">2, 1</td><td style = "width:33%; text-align:left">4, 3, 2, 1</td></tr>
#> <tr><td style = "width:33%; text-align:left">5</td><td style = "width:33%; text-align:left">2, 1</td><td style = "width:33%; text-align:left">3, 2, 1, 4</td></tr>
#> <tr><td style = "width:33%; text-align:left">6</td><td style = "width:33%; text-align:left">2, 1</td><td style = "width:33%; text-align:left">4, 3, 1, 2</td></tr>
#> <tr><td style = "width:33%; text-align:left">7</td><td style = "width:33%; text-align:left">2, 1</td><td style = "width:33%; text-align:left">4, 3, 2, 1</td></tr>
#> <tr><td style = "width:33%; text-align:left">8</td><td style = "width:33%; text-align:left">2, 1</td><td style = "width:33%; text-align:left">3, 4, 2, 1</td></tr>
#> <tr><td style = "width:33%; text-align:left">NA</td><td style = "width:33%; text-align:left">2, NA</td><td style = "width:33%; text-align:left">3, NA</td></tr>
#> </table></body></html>
#>                     <html style="font-family:Helvetica,Arial,Sans">
#>                     <head><title>JudgeID Label Table</title><style type = "text/css">
#>                     p {
#>                     font-size:smaller;
#>                     }
#>                     table {
#>                     border: 0px;
#>                     border-collapse:collapse;
#>                     font-size:smaller;
#>                     table-layout:fixed;
#>                     margin-left:0%;
#>                     margin-right:auto;
#>                     }
#>                     .headtab {
#>                     width: 100%;
#>                     margin-left:auto;
#>                     margin-right:auto;
#>                     }
#>                     th {
#>                     background-color: #FFFFFF;
#>                     font-weight:bold;
#>                     text-align:left;
#>                     }
#>                     table tr:nth-child(odd) td {
#>                     background-color: #FFFFFF;
#>                     padding:4px;
#>                     word-wrap: break-word;
#>                     word-break:break-all;
#>                     }
#>                     table tr:nth-child(even) td {
#>                     background-color: #D3D3D3;
#>                     padding:4px;
#>                     word-wrap: break-word;
#>                     word-break:break-all;
#>                     }</style></head><body> <table class="headtab"> <tr><td style="text-align:left">labeltable {vtable}</td> <td style="text-align:right">Variable Documentation: Label Table</td></tr></table> <h1> JudgeID </h1><h3>Label Table</h3><table><tr><th style = "width:50%; text-align:left">JudgeID</th><th style = "width:50%; text-align:left">Judge</th></tr>
#> <tr><td style = "width:50%; text-align:left">1</td><td style = "width:50%; text-align:left">AARONSON,L.H.</td></tr>
#> <tr><td style = "width:50%; text-align:left">10</td><td style = "width:50%; text-align:left">DANNEHY,J.F.</td></tr>
#> <tr><td style = "width:50%; text-align:left">11</td><td style = "width:50%; text-align:left">DEAN,H.H.</td></tr>
#> <tr><td style = "width:50%; text-align:left">12</td><td style = "width:50%; text-align:left">DEVITA,H.J.</td></tr>
#> <tr><td style = "width:50%; text-align:left">13</td><td style = "width:50%; text-align:left">DRISCOLL,P.J.</td></tr>
#> <tr><td style = "width:50%; text-align:left">14</td><td style = "width:50%; text-align:left">GRILLO,A.E.</td></tr>
#> <tr><td style = "width:50%; text-align:left">15</td><td style = "width:50%; text-align:left">HADDEN,W.L.JR.</td></tr>
#> <tr><td style = "width:50%; text-align:left">16</td><td style = "width:50%; text-align:left">HAMILL,E.C.</td></tr>
#> <tr><td style = "width:50%; text-align:left">17</td><td style = "width:50%; text-align:left">HEALEY.A.H.</td></tr>
#> <tr><td style = "width:50%; text-align:left">18</td><td style = "width:50%; text-align:left">HULL,T.C.</td></tr>
#> <tr><td style = "width:50%; text-align:left">19</td><td style = "width:50%; text-align:left">LEVINE,I.</td></tr>
#> <tr><td style = "width:50%; text-align:left">2</td><td style = "width:50%; text-align:left">ALEXANDER,J.M.</td></tr>
#> <tr><td style = "width:50%; text-align:left">20</td><td style = "width:50%; text-align:left">LEVISTER,R.L.</td></tr>
#> <tr><td style = "width:50%; text-align:left">21</td><td style = "width:50%; text-align:left">MARTIN,L.F.</td></tr>
#> <tr><td style = "width:50%; text-align:left">22</td><td style = "width:50%; text-align:left">MCGRATH,J.F.</td></tr>
#> <tr><td style = "width:50%; text-align:left">23</td><td style = "width:50%; text-align:left">MIGNONE,A.F.</td></tr>
#> <tr><td style = "width:50%; text-align:left">24</td><td style = "width:50%; text-align:left">MISSAL,H.M.</td></tr>
#> <tr><td style = "width:50%; text-align:left">25</td><td style = "width:50%; text-align:left">MULVEY,H.M.</td></tr>
#> <tr><td style = "width:50%; text-align:left">26</td><td style = "width:50%; text-align:left">NARUK,H.J.</td></tr>
#> <tr><td style = "width:50%; text-align:left">27</td><td style = "width:50%; text-align:left">O'BRIEN,F.J.</td></tr>
#> <tr><td style = "width:50%; text-align:left">28</td><td style = "width:50%; text-align:left">O'SULLIVAN,T.J.</td></tr>
#> <tr><td style = "width:50%; text-align:left">29</td><td style = "width:50%; text-align:left">PASKEY,L.</td></tr>
#> <tr><td style = "width:50%; text-align:left">3</td><td style = "width:50%; text-align:left">ARMENTANO,A.J.</td></tr>
#> <tr><td style = "width:50%; text-align:left">30</td><td style = "width:50%; text-align:left">RUBINOW,J.E.</td></tr>
#> <tr><td style = "width:50%; text-align:left">31</td><td style = "width:50%; text-align:left">SADEN.G.A.</td></tr>
#> <tr><td style = "width:50%; text-align:left">32</td><td style = "width:50%; text-align:left">SATANIELLO,A.G.</td></tr>
#> <tr><td style = "width:50%; text-align:left">33</td><td style = "width:50%; text-align:left">SHEA,D.M.</td></tr>
#> <tr><td style = "width:50%; text-align:left">34</td><td style = "width:50%; text-align:left">SHEA,J.F.JR.</td></tr>
#> <tr><td style = "width:50%; text-align:left">35</td><td style = "width:50%; text-align:left">SIDOR,W.J.</td></tr>
#> <tr><td style = "width:50%; text-align:left">36</td><td style = "width:50%; text-align:left">SPEZIALE,J.A.</td></tr>
#> <tr><td style = "width:50%; text-align:left">37</td><td style = "width:50%; text-align:left">SPONZO,M.J.</td></tr>
#> <tr><td style = "width:50%; text-align:left">38</td><td style = "width:50%; text-align:left">STAPLETON,J.F.</td></tr>
#> <tr><td style = "width:50%; text-align:left">39</td><td style = "width:50%; text-align:left">TESTO,R.J.</td></tr>
#> <tr><td style = "width:50%; text-align:left">4</td><td style = "width:50%; text-align:left">BERDON,R.I.</td></tr>
#> <tr><td style = "width:50%; text-align:left">40</td><td style = "width:50%; text-align:left">TIERNEY,W.L.JR.</td></tr>
#> <tr><td style = "width:50%; text-align:left">41</td><td style = "width:50%; text-align:left">WALL,R.A.</td></tr>
#> <tr><td style = "width:50%; text-align:left">42</td><td style = "width:50%; text-align:left">WRIGHT,D.B.</td></tr>
#> <tr><td style = "width:50%; text-align:left">43</td><td style = "width:50%; text-align:left">ZARRILLI,K.J.</td></tr>
#> <tr><td style = "width:50%; text-align:left">5</td><td style = "width:50%; text-align:left">BRACKEN,J.J.</td></tr>
#> <tr><td style = "width:50%; text-align:left">6</td><td style = "width:50%; text-align:left">BURNS,E.B.</td></tr>
#> <tr><td style = "width:50%; text-align:left">7</td><td style = "width:50%; text-align:left">CALLAHAN,R.J.</td></tr>
#> <tr><td style = "width:50%; text-align:left">8</td><td style = "width:50%; text-align:left">COHEN,S.S.</td></tr>
#> <tr><td style = "width:50%; text-align:left">9</td><td style = "width:50%; text-align:left">DALY,J.J.</td></tr>
#> </table></body></html>
if(interactive()){
#Input a single labelled variable to see a table relating values to labels.
#Values not present in the data will be included in the table but moved to the end.
library(sjlabelled)
data(efc)
labeltable(efc$e15relat)

#Include multiple variables to see, for each value of the first variable,
#each value of the others present in the data.
data(efc)
labeltable(efc$e15relat,efc$e16sex,efc$e42dep)

#Commonly, the multi-variable version might be used to recover the original
#values of encoded variables
data(USJudgeRatings)
USJudgeRatings$Judge <- row.names(USJudgeRatings)
USJudgeRatings$JudgeID <- as.numeric(as.factor(USJudgeRatings$Judge))
labeltable(USJudgeRatings$JudgeID,USJudgeRatings$Judge)
}