<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/tagged-na-values-and-labelled-data-rstats/"></div></div><div><a data-url="https://www.r-bloggers.com/tagged-na-values-and-labelled-data-rstats/" data-counturl="https://www.r-bloggers.com/tagged-na-values-and-labelled-data-rstats/" data-count="horizontal" data-text="Tagged NA values and labelled data #rstats" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://strengejacke.wordpress.com/2016/09/27/tagged-na-values-and-labelled-data-rstats/"> R – Strenge Jacke!</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h1>sjmisc-package: Working with labelled data</h1>
<p>A major update of my <a target="_blank" href="https://cran.r-project.org/package=sjmisc">sjmisc-package</a> was just released an CRAN. A major change (see <a target="_blank" href="https://github.com/sjPlot/sjmisc/releases/tag/2.0.0">changelog for all changes</a> )is the support of the latest release from the <a target="_blank" href="https://cran.r-project.org/package=haven">haven-package</a>, a package to import and export SPSS, SAS or Stata files.</p>
<p>The <em>sjmisc-package</em> mainly addresses three domains:</p>
<ul>
<li>reading and writing data between other statistical packages and R</li>
<li>functions to make working with labelled data easier</li>
<li>frequently applied recoding and variable transformation tasks, also with support for labelled data</li>
</ul>
<p>In this post, I want to introduce the topic of labelled data and give some examples of what the <em>sjmisc-package</em> can do, with a special focus on <strong>tagged NA values</strong>.</p>
<h1>Introduction into Labelled Data</h1>
<p>Labelled data (or labelled vectors) is a common data structure in other statistical environments to store meta-information about variables, like variable names, value labels or multiple defined missing values. Labelled data not only extends R‘s capabilities to deal with proper value and variable labels, but also facilitates the representation of different types of missing values, like in other statistical software packages. Typically, in R, multiple declared missings cannot be represented in a similar way, like in ’SPSS’ or ‘SAS’, with the regular missing values. However, Hadley Wickham’s <em>haven package</em> introduced <code>tagged_na</code> values, which can do this. Tagged NA’s work exactly like regular R missing values except that they store one additional byte of information: a tag, which is usually a letter (“a” to “z”) or also may be a character number (“0” to “9”). This allows to indicate different missings.</p>
<pre>library(haven)
x &lt;- labelled(
  c(1:3, tagged_na(&quot;a&quot;, &quot;c&quot;, &quot;z&quot;), 4:1),
  c(&quot;Agreement&quot; = 1, &quot;Disagreement&quot; = 4, 
    &quot;First&quot; = tagged_na(&quot;c&quot;),
    &quot;Refused&quot; = tagged_na(&quot;a&quot;), 
    &quot;Not home&quot; = tagged_na(&quot;z&quot;))
)

print(x)
# &lt;Labelled double>
#  [1]     1     2     3 NA(a) 
#  [5] NA(c) NA(z)     4     3     2     1
# 
# Labels:
#  value        label
#      1    Agreement
#      4 Disagreement
#  NA(c)        First
#  NA(a)      Refused
#  NA(z)     Not home</pre>
<h1>Value Labels</h1>
<h2>Getting value labels</h2>
<p>The <code>get_labels()</code>-method is a generic method to return value labels of a vector or data frame.</p>
<pre>get_labels(efc$e42dep)
# [1] &quot;independent&quot;          &quot;slightly dependent&quot;
# [3] &quot;moderately dependent&quot; &quot;severely dependent&quot;</pre>
<p>You can prefix the value labels with the associated values or return them as named vector with the <code>include.values</code> argument.</p>
<pre>get_labels(efc$e42dep, include.values = &quot;p&quot;)
# [1] &quot;[1] independent&quot;          &quot;[2] slightly dependent&quot;  
# [3] &quot;[3] moderately dependent&quot; &quot;[4] severely dependent&quot;</pre>
<p><code>get_labels()</code> also returns “labels” of factors, even if the factor has no label attributes. This is useful, if you need a generic method for your functions to get value labels, either for labelled data or for factors.</p>
<pre>x &lt;- factor(c(&quot;low&quot;, &quot;mid&quot;, &quot;low&quot;, &quot;hi&quot;, &quot;mid&quot;, &quot;low&quot;))
get_labels(x)
# [1] &quot;hi&quot;  &quot;low&quot; &quot;mid&quot;</pre>
<p>Tagged missing values can also be included in the output, using the <code>drop.na</code> argument.</p>
<pre># get labels, including tagged NA values
x &lt;- labelled(
  c(1:3, tagged_na(&quot;a&quot;, &quot;c&quot;, &quot;z&quot;), 4:1),
  c(&quot;Agreement&quot; = 1, &quot;Disagreement&quot; = 4, 
    &quot;First&quot; = tagged_na(&quot;c&quot;),
    &quot;Refused&quot; = tagged_na(&quot;a&quot;), 
    &quot;Not home&quot; = tagged_na(&quot;z&quot;))
)

get_labels(x, include.values = &quot;n&quot;, drop.na = FALSE)
#              1              4          
#    &quot;Agreement&quot; &quot;Disagreement&quot;
#
#      NA(c)          NA(a)          NA(z) 
#    &quot;First&quot;      &quot;Refused&quot;     &quot;Not home&quot;</pre>
<h2>Getting labelled values</h2>
<p>The <code>get_values()</code> method returns the values for labelled values (i.e. values that have an associated label). We still use the vector x from the above examples.</p>
<pre>print(x)
# 1 4 NA NA NA 
#  [1]  1  2  3 NA NA NA  4  3  2  1
# attr(,&quot;labels&quot;)
#    Agreement Disagreement        First      Refused     Not home 
#            1            4           NA           NA           NA

get_values(x)
# [1] &quot;1&quot;     &quot;4&quot;     &quot;NA(c)&quot; &quot;NA(a)&quot; &quot;NA(z)&quot;</pre>
<p>With the drop.na argument you can omit those values from the return values that are defined as missing.</p>
<pre>get_values(x, drop.na = TRUE)
# [1] 1 4</pre>
<h2>Setting value labels</h2>
<p>With <code>set_labels()</code> you can add label attributes to any vector. You can either return a new labelled vector, or label an existing vector.</p>
<pre>x &lt;- sample(1:4, 20, replace = TRUE)

# return new labelled vector
x &lt;- set_labels(x, c(&quot;very low&quot;, &quot;low&quot;, &quot;mid&quot;, &quot;hi&quot;))
x
#  [1] 4 2 1 2 4 1 3 1 3 1 1 4 2 4 2 4 3 4 4 4
# attr(,&quot;labels&quot;)
# very low      low      mid       hi 
#        1        2        3        4

# label existing vector
set_labels(x) &lt;- c(&quot;too low&quot;, &quot;less low&quot;, 
                   &quot;mid&quot;, &quot;very hi&quot;)
x
#  [1] 4 2 1 2 4 1 3 1 3 1 1 4 2 4 2 4 3 4 4 4
# attr(,&quot;labels&quot;)
#  too low less low      mid  very hi 
#        1        2        3        4</pre>
<p>To add explicit labels for values, use a named vector of labels as argument.</p>
<pre>x &lt;- c(1, 2, 3, 2, 4, 5)
x &lt;- set_labels(x, c(&quot;strongly agree&quot; = 1, 
                     &quot;totally disagree&quot; = 4, 
                     &quot;refused&quot; = 5,
                     &quot;missing&quot; = 9))
x
# [1] 1 2 3 2 4 5
# attr(,&quot;labels&quot;)
#   strongly agree totally disagree          refused          missing 
#                1                4                5                9</pre>
<h1>Missing Values</h1>
<h2>Defining missing values</h2>
<p><code>set_na()</code> converts values of a vector or of multiple vectors in a data frame into tagged NAs, which means that these missing values get an information tag and a value label (which is, by default, the former value that was converted to NA). You can either return a new vector/data frame, or set NAs into an existing vector/data frame.</p>
<pre>x &lt;- sample(1:8, 100, replace = TRUE)
table(x)
# x
#  1  2  3  4  5  6  7  8 
# 10 12  6 13 12 17 18 12

set_na(x) &lt;- c(1, 8)
x
#   [1]  2  6  6 NA  7  4 NA  3  6 NA  4 NA  5  4  2  5  2  2  3  2  5  6 NA
#  [24]  7  6  4  6  3  4 NA NA  5 NA  6 NA  7  7  7  6  6 NA  7  2  2 NA  6
#  [47]  4  6  5  7  5 NA NA  7  4  7  4  3  7  2  6  5  5  7  2 NA  6  6 NA
#  [70]  2  5  7  4  7 NA  2  7  7  7  4  6  3 NA  5  5 NA  7  4  3  4 NA  6
#  [93]  4  2 NA NA  6  7  5 NA
# attr(,&quot;labels&quot;)
#  1  8 
# NA NA

table(x, useNA = &quot;always&quot;)
# x
#    2    3    4    5    6    7 &lt;NA> 
#   12    6   13   12   17   18   22

print_tagged_na(x)
#   [1]     2     6     6 NA(8)     7     4 NA(1)     3     6 NA(1)     4
#  [12] NA(8)     5     4     2     5     2     2     3     2     5     6
#  [23] NA(8)     7     6     4     6     3     4 NA(1) NA(1)     5 NA(1)
#  [34]     6 NA(8)     7     7     7     6     6 NA(1)     7     2     2
#  [45] NA(1)     6     4     6     5     7     5 NA(8) NA(8)     7     4
#  [56]     7     4     3     7     2     6     5     5     7     2 NA(8)
#  [67]     6     6 NA(8)     2     5     7     4     7 NA(1)     2     7
#  [78]     7     7     4     6     3 NA(1)     5     5 NA(8)     7     4
#  [89]     3     4 NA(8)     6     4     2 NA(8) NA(8)     6     7     5
# [100] NA(1)

x &lt;- factor(c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;))
x
# [1] a b c
# Levels: a b c

set_na(x) &lt;- &quot;b&quot; 
x
# [1] a    &lt;NA> c   
# attr(,&quot;labels&quot;)
#  b 
# NA 
# Levels: a b c</pre>
<h2>Getting missing values</h2>
<p>The <code>get_na()</code> function returns all tagged NA values.</p>
<pre>set_na(efc$c87cop6) &lt;- 3
get_na(efc$c87cop6)
# Often 
#    NA

get_na(efc$c87cop6, as.tag = TRUE)
#   Often 
# &quot;NA(3)&quot;</pre>
<h2>Replacing specific NA with values</h2>
<p>While <code>set_na()</code> allows you to replace values with specific tagged NA’s, <code>replace_na()</code> allows you to replace either all NA values of a vector or specific tagged NA values with a non-NA value.</p>
<pre>str(efc$c84cop3)
#  atomic [1:908] 2 3 1 3 1 3 4 2 3 1 ...
#  - attr(*, &quot;label&quot;)= chr &quot;does caregiving cause difficulties in your relationship with your friends?&quot;
#  - attr(*, &quot;labels&quot;)= Named num [1:4] 1 2 3 4
#   ..- attr(*, &quot;names&quot;)= chr [1:4] &quot;Never&quot; &quot;Sometimes&quot; &quot;Often&quot; &quot;Always&quot;

set_na(efc$c84cop3) &lt;- c(2, 3)
str(efc$c84cop3)
#  atomic [1:908] NA NA 1 NA 1 NA 4 NA NA 1 ...
#  - attr(*, &quot;label&quot;)= chr &quot;does caregiving cause difficulties in your relationship with your friends?&quot;
#  - attr(*, &quot;labels&quot;)= Named num [1:4] 1 NA NA 4
#   ..- attr(*, &quot;names&quot;)= chr [1:4] &quot;Never&quot; &quot;Sometimes&quot; &quot;Often&quot; &quot;Always&quot;

get_na(efc$c84cop3)
# Sometimes     Often 
#        NA        NA

replace_na(efc$c84cop3, na.label = &quot;restored NA&quot;, tagged.na = &quot;2&quot;) &lt;- 2
str(efc$c84cop3)
#  atomic [1:908] 2 NA 1 NA 1 NA 4 2 NA 1 ...
#  - attr(*, &quot;label&quot;)= chr &quot;does caregiving cause difficulties in your relationship with your friends?&quot;
#  - attr(*, &quot;labels&quot;)= Named num [1:4] 1 2 4 NA
#   ..- attr(*, &quot;names&quot;)= chr [1:4] &quot;Never&quot; &quot;restored NA&quot; &quot;Always&quot; &quot;Often&quot;

get_na(efc$c84cop3)
# Often 
#    NA

get_labels(efc$c84cop3, include.values = &quot;p&quot;)
# [1] &quot;[1] Never&quot;       &quot;[2] restored NA&quot; &quot;[4] Always&quot;</pre>
<h1>Conclusions</h1>
<p>Labelled data vastly extends R‘s capabilities to deal with value and variable labels. The <em>sjmisc-package</em> offers a collection of convenient functions to work with labelled data, which might be of interest especially for users coming from other statistical packages like SPSS, who want to switch to R. Packages like <a target="_blank" href="https://cran.r-project.org/package=sjPlot">sjPlot</a> facilitate the features of labelled data, making it easy to produce well annotated plots (see <a target="_blank" href="http://strengejacke.de/sjPlot/">these vignettes</a> for various examples). A slightly more comprehensive introduction into the <code>sjmisc-package</code> can be <a target="_blank" href="http://strengejacke.de/sjPlot/sjmisc/">found here</a>.</p>
<p> Tagged: <a target="_blank" href="https://strengejacke.wordpress.com/tag/labelled-data/">labelled data</a>, <a target="_blank" href="https://strengejacke.wordpress.com/tag/r/">R</a>, <a target="_blank" href="https://strengejacke.wordpress.com/tag/rstats/">rstats</a>, <a target="_blank" href="https://strengejacke.wordpress.com/tag/sjplot/">sjPlot</a>, <a target="_blank" href="https://strengejacke.wordpress.com/tag/spss/">SPSS</a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/strengejacke.wordpress.com/1142/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/strengejacke.wordpress.com/1142/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/tagged-na-values-and-labelled-data-rstats/"></div></div><div><a data-url="https://www.r-bloggers.com/tagged-na-values-and-labelled-data-rstats/" data-counturl="https://www.r-bloggers.com/tagged-na-values-and-labelled-data-rstats/" data-count="vertical" data-text="Tagged NA values and labelled data #rstats" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://strengejacke.wordpress.com/2016/09/27/tagged-na-values-and-labelled-data-rstats/"> R – Strenge Jacke!</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/woOhtWyiKYY"></a></div>