<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/how-to-use-dplyrs-mutate-in-r-without-a-vectorized-function/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://deanattali.com/blog/mutate-non-vectorized/"> Dean Attali's R Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>TL;DR: Use the <code>Vectorize()</code> function!</strong></p>
<p>If you’re reading this, you’ve either encountered this problem before, or you just got to this article out of curiousity (in which case you probably don’t know what problem I’m talking about).</p>
<p>A few days ago I was given code by a client for a function that, given a path to a patient’s file, generates a useful ID for the patient. I won’t post the actual function, but it was something along the lines of this:</p>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>stringr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>patient_name</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>path</span><span>)</span><span> </span><span>{</span><span>
</span><span>path_list</span><span> </span><span>&lt;-</span><span> </span><span>str_split</span><span>(</span><span>path</span><span>,</span><span> </span><span>&quot;/&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>unlist</span><span>()</span><span>
</span><span>paste</span><span>(</span><span>path_list</span><span>[</span><span>length</span><span>(</span><span>path_list</span><span>)</span><span> </span><span>-</span><span> </span><span>1</span><span>],</span><span> </span><span>path_list</span><span>[</span><span>length</span><span>(</span><span>path_list</span><span>)],</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;_&quot;</span><span>)</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
</div>
<p>Given a path <code>some/path/abc/001.txt</code>, this function will return <code>abc_001.txt</code>. So far, so good. (There are better ways to implement this function, but that’s not the point here).</p>
<p>I had a dataframe with file paths as a column, and I needed to add this ID as another column. Normally, this would be easily achieved with a simple <code>mutate()</code> (from <code>dplyr</code>):</p>
<div>
<div>
<pre><code><span>df</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>path</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;some/path/abc/001.txt&quot;</span><span>,</span><span> </span><span>&quot;another/directory/xyz/002.txt&quot;</span><span>),</span><span>
</span><span>stringsAsFactors</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>df</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>patient_name</span><span> </span><span>=</span><span> </span><span>patient_name</span><span>(</span><span>path</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<p>This code does run, but it’s not correct. Here’s the output:</p>
<div>
<div>
<pre><code>                           path patient_name
1         some/path/abc/001.txt  xyz_002.txt
2 another/directory/xyz/002.txt  xyz_002.txt
</code></pre>
</div>
</div>
<p>You can see the result is incorrect, and it’s because <code>patient_name()</code> is not a vectorized function – it assumes that its input is just a single path, and doesn’t know how to properly work when given multiple paths.</p>
<p>Usually when I write my own code, I try to make my functions vectorized, so that you can call them with both a single element or with a vector. But in this case, I wasn’t allowed to modify the code to make the function vectorized. So how do we easily vectorize <code>patient_name()</code> without modifying its code? We can use the <code>Vectorize()</code> function!</p>
<div>
<div>
<pre><code><span>patient_name_v</span><span> </span><span>&lt;-</span><span> </span><span>Vectorize</span><span>(</span><span>patient_name</span><span>)</span><span>
</span><span>df</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>patient_name</span><span> </span><span>=</span><span> </span><span>patient_name_v</span><span>(</span><span>path</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<p>This actually gives us the correct result:</p>
<div>
<div>
<pre><code>                           path patient_name
1         some/path/abc/001.txt  abc_001.txt
2 another/directory/xyz/002.txt  xyz_002.txt
</code></pre>
</div>
</div>
<p>This is only one usecase of the <code>Vectorize()</code> function. It can come in handy whenever you need to vectorize a non-vectorized function.</p>
<p>For example, it seems that the <code>nrow()</code> function is not vectorized, because if I try to create a list with two dataframes in it and get the number of rows, I get <code>NULL</code>:</p>
<div>
<div>
<pre><code><span>dflist</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>iris</span><span>)</span><span>
</span><span>nrow</span><span>(</span><span>dflist</span><span>)</span><span>
</span><span># NULL</span><span>
</span></code></pre>
</div>
</div>
<p>I hypothesized that vectorizing it will do the trick, and it indeed seems to work!</p>
<div>
<div>
<pre><code><span>(</span><span>Vectorize</span><span>(</span><span>nrow</span><span>))(</span><span>dflist</span><span>)</span><span>
</span><span># [1] 32 150</span><span>
</span></code></pre>
</div>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://deanattali.com/blog/mutate-non-vectorized/"> Dean Attali's R 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/xffTARffFW0" height="1"></a></div>