<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/r-tip-introduce-indices-to-avoid-for-class-loss-issues/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2018/03/r-tip-introduce-indices-to-avoid-for-class-loss-issues/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Here is an <a rel="nofollow" href="https://www.r-project.org/" target="_blank">R</a> <a rel="nofollow" href="http://www.win-vector.com/blog/tag/r-tips/" target="_blank">tip</a>. Use loop indices to avoid <code>for()</code>-loops damaging classes.</p>
<p>Below is an R annoyance that occurs again and again: vectors lose class attributes when you iterate over them in a <code>for()</code>-loop.</p>
<pre>
d &lt;- c(Sys.time(), Sys.time())
print(d)
#> [1] &quot;2018-02-18 10:16:16 PST&quot; &quot;2018-02-18 10:16:16 PST&quot;

for(di in d) {
  print(di)
}
#> [1] 1518977777
#> [1] 1518977777
</pre>
<p>Notice we printed numbers, not dates/times. To avoid this problem introduce an index, and loop over that, not over the vector contents.</p>
<pre>
for(ii in seq_along(d)) {
  di &lt;- d[[ii]]
  print(di)
}
#> [1] &quot;2018-02-18 10:16:16 PST&quot;
#> [1] &quot;2018-02-18 10:16:16 PST&quot;
</pre>
<p><span></span></p>
<p><code>seq_along()</code> is a handy function similar to what we discussed in <a rel="nofollow" href="http://www.win-vector.com/blog/2018/02/r-tip-use-seq_len-to-avoid-the-backwards-sequence-bug/" target="_blank">R Tip: Use <code>seq_len()</code> to Avoid The Backwards List Trap</a>.</p>
<p>The introduction of indices <em>is</em> ugly, as index-free iteration is <em>generally</em> superior. Also, <a rel="nofollow" href="http://www.win-vector.com/blog/2018/03/r-tip-use-vectormode-list-to-pre-allocate-lists/" target="_blank">as we have mentioned before</a>, <code>for</code>-loops should <em>not</em> be considered anathema in <code>R</code>– they are a useful tool when used correctly.</p>
<p>Note <code>base::ifelse()</code> also loses class attributes, though <code>dplyr::if_else()</code> avoids the problem. Also <code>base::lapply()</code> and <code>base::vapply()</code> do not have the problem (for example try: <code>vapply(d, as.character, character(1))</code> and <code>lapply(d, class)</code>).</p>
<p>In both cases <code>R</code> is treating a vector of numbers as a complex class by adding a <code>class</code> <code>attr</code> to the vector. This means the vector is a <em>single</em> object holding multiple times, <em>not</em> a list of individual time objects. Any subsetting that strips <code>attr</code> values loses the class information and the derived vector reverts to its underlying type (in this case <code>double</code>).</p>
<p>For pre-allocation ideas (an important compliment to <code>for</code>-loops) please see <a rel="nofollow" href="http://www.win-vector.com/blog/2018/03/r-tip-use-vectormode-list-to-pre-allocate-lists/" target="_blank">R Tip: Use <code>vector()</code> to Pre-Allocate Lists</a> (also includes some discussion of <code>for</code>-loops).</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.win-vector.com/blog/2018/03/r-tip-introduce-indices-to-avoid-for-class-loss-issues/"> R – Win-Vector Blog</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/_HzOh--CuFY" height="1"></a></div>