<p>Over the couple of months there have been a bunch of smaller releases to packages in the <a target="_blank" rel="nofollow" href="http://tidyverse.org/">tidyverse</a>. This includes:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="http://forcats.tidyverse.org/">forcats</a> 0.2.0, for working with factors.</li>
<li><a target="_blank" rel="nofollow" href="http://readr.tidyverse.org/">readr</a> 1.1.0, for reading flat-files from disk.</li>
<li><a target="_blank" rel="nofollow" href="http://stringr.tidyverse.org/">stringr</a> 1.2.0, for manipulating strings.</li>
<li><a target="_blank" rel="nofollow" href="http://tibble.tidyverse.org/">tibble</a> 1.3.0, a modern re-imagining of the data frame.</li>
</ul>
<p>This blog post summarises the most important new features, and points to the full release notes where you can learn more.</p>
<p>(If you’ve never heard of the tidyverse before, it’s an set of packages that are designed to work together to help you do data science. The best place to learn all about it is <a target="_blank" rel="nofollow" href="http://r4ds.had.co.nz/">R for Data Science</a>.)</p>
<div>
<h2>forcats 0.2.0</h2>
<p>forcats has three new functions:</p>
<ul>
<li><code>as_factor()</code> is a generic version of <code>as.factor()</code>, which creates factors from character vectors ordered by appearance, rather than alphabetically. This ensures means that <code>as_factor(x)</code> will always return the same result, regardless of the current locale.</li>
<li><code>fct_other()</code> makes it easier to convert selected levels to “other”:
<pre><code><span>x</span> <span>&lt;-</span> <span>factor</span><span>(</span><span>rep</span><span>(</span><span>LETTERS</span><span>[</span><span>1</span><span>:</span><span>6</span><span>]</span>, <span>times</span> <span>=</span> <span>c</span><span>(</span><span>10</span>, <span>5</span>, <span>1</span>, <span>1</span>, <span>1</span>, <span>1</span><span>)</span><span>)</span><span>)</span>

<span>x</span> <span>%>%</span> 
  <span>fct_other</span><span>(</span><span>keep</span> <span>=</span> <span>c</span><span>(</span><span>&quot;A&quot;</span>, <span>&quot;B&quot;</span><span>)</span><span>)</span> <span>%>%</span> 
  <span>fct_count</span><span>(</span><span>)</span>
<span>#> # A tibble: 3 × 2</span>
<span>#> f n</span>
<span>#> </span>
<span>#> 1 A 10</span>
<span>#> 2 B 5</span>
<span>#> 3 Other 4</span>

<span>x</span> <span>%>%</span> 
  <span>fct_other</span><span>(</span><span>drop</span> <span>=</span> <span>c</span><span>(</span><span>&quot;A&quot;</span>, <span>&quot;B&quot;</span><span>)</span><span>)</span> <span>%>%</span> 
  <span>fct_count</span><span>(</span><span>)</span>
<span>#> # A tibble: 5 × 2</span>
<span>#> f n</span>
<span>#> </span>
<span>#> 1 C 1</span>
<span>#> 2 D 1</span>
<span>#> 3 E 1</span>
<span>#> 4 F 1</span>
<span>#> 5 Other 15</span></code></pre>
</li>
<li><code>fct_relabel()</code> allows programmatic relabeling of levels:
<pre><code><span>x</span> <span>&lt;-</span> <span>factor</span><span>(</span><span>letters</span><span>[</span><span>1</span><span>:</span><span>3</span><span>]</span><span>)</span>
<span>x</span>
<span>#> [1] a b c</span>
<span>#> Levels: a b c</span>

<span>x</span> <span>%>%</span> <span>fct_relabel</span><span>(</span><span>function</span><span>(</span><span>x</span><span>)</span> <span>paste0</span><span>(</span><span>&quot;-&quot;</span>, <span>x</span>, <span>&quot;-&quot;</span><span>)</span><span>)</span>
<span>#> [1] -a- -b- -c-</span>
<span>#> Levels: -a- -b- -c-</span></code></pre>
</li>
</ul>
<p>See the full list of other changes in the <a target="_blank" rel="nofollow" href="https://github.com/tidyverse/forcats/releases/tag/v0.2.0">release notes</a>.</p>
</div>
<div>
<h2>stringr 1.2.0</h2>
<p>This release includes a change to the API: <code>str_match_all()</code> now returns NA if an optional group doesn’t match (previously it returned “”). This is more consistent with <code>str_match()</code> and other match failures.</p>
<pre><code><span>x</span> <span>&lt;-</span> <span>c</span><span>(</span><span>&quot;a=1,b=2&quot;</span>, <span>&quot;c=3&quot;</span>, <span>&quot;d=&quot;</span><span>)</span>

<span>x</span> <span>%>%</span> <span>str_match</span><span>(</span><span>&quot;(.)=(\\d)?&quot;</span><span>)</span>
<span>#> [,1] [,2] [,3]</span>
<span>#> [1,] &quot;a=1&quot; &quot;a&quot; &quot;1&quot; </span>
<span>#> [2,] &quot;c=3&quot; &quot;c&quot; &quot;3&quot; </span>
<span>#> [3,] &quot;d=&quot; &quot;d&quot; NA</span>
<span>x</span> <span>%>%</span> <span>str_match_all</span><span>(</span><span>&quot;(.)=(\\d)?,?&quot;</span><span>)</span>
<span>#> [[1]]</span>
<span>#> [,1] [,2] [,3]</span>
<span>#> [1,] &quot;a=1,&quot; &quot;a&quot; &quot;1&quot; </span>
<span>#> [2,] &quot;b=2&quot; &quot;b&quot; &quot;2&quot; </span>
<span>#> </span>
<span>#> [[2]]</span>
<span>#> [,1] [,2] [,3]</span>
<span>#> [1,] &quot;c=3&quot; &quot;c&quot; &quot;3&quot; </span>
<span>#> </span>
<span>#> [[3]]</span>
<span>#> [,1] [,2] [,3]</span>
<span>#> [1,] &quot;d=&quot; &quot;d&quot; NA</span></code></pre>
<p>There are three new features:</p>
<ul>
<li>In <code>str_replace()</code>, <code>replacement</code> can now be a function. The function is once for each match and its return value will be used as the replacement.
<pre><code><span>redact</span> <span>&lt;-</span> <span>function</span><span>(</span><span>x</span><span>)</span> <span>{</span>
  <span>str_dup</span><span>(</span><span>&quot;-&quot;</span>, <span>str_length</span><span>(</span><span>x</span><span>)</span><span>)</span>
<span>}</span>

<span>x</span> <span>&lt;-</span> <span>c</span><span>(</span><span>&quot;It cost $500&quot;</span>, <span>&quot;We spent $1,200 on stickers&quot;</span><span>)</span>
<span>x</span> <span>%>%</span> <span>str_replace_all</span><span>(</span><span>&quot;\\$[0-9,]+&quot;</span>, <span>redact</span><span>)</span>
<span>#> [1] &quot;It cost ----&quot; &quot;We spent ------ on stickers&quot;</span></code></pre>
</li>
<li>New <code>str_which()</code> mimics <code>grep()</code>:
<pre><code><span>fruit</span> <span>&lt;-</span> <span>c</span><span>(</span><span>&quot;apple&quot;</span>, <span>&quot;banana&quot;</span>, <span>&quot;pear&quot;</span>, <span>&quot;pinapple&quot;</span><span>)</span>

<span># Matching positions </span>
<span>str_which</span><span>(</span><span>fruit</span>, <span>&quot;p&quot;</span><span>)</span>
<span>#> [1] 1 3 4</span>

<span># Matching values</span>
<span>str_subset</span><span>(</span><span>fruit</span>, <span>&quot;p&quot;</span><span>)</span>
<span>#> [1] &quot;apple&quot; &quot;pear&quot; &quot;pinapple&quot;</span></code></pre>
</li>
<li>A new vignette (<a target="_blank" rel="nofollow" href="http://stringr.tidyverse.org/articles/regular-expressions.html"><code>vignette(&quot;regular-expressions&quot;)</code></a>) describes the details of the regular expressions supported by stringr. The main vignette (<a target="_blank" rel="nofollow" href="http://stringr.tidyverse.org/articles/stringr.html"><code>vignette(&quot;stringr&quot;)</code></a>) has been updated to give a high-level overview of the package.</li>
</ul>
<p>See the full list of other changes in the <a target="_blank" rel="nofollow" href="https://github.com/tidyverse/stringr/releases/tag/v1.2.0">release notes</a>.</p>
</div>
<div>
<h2>readr 1.1.0</h2>
<p>readr gains two new features:</p>
<ul>
<li>All <code>write_*()</code> functions now support connections. This means that that you can write directly to compressed formats such as <code>.gz</code>, <code>bz2</code> or <code>.xz</code> (and readr will automatically do so if you use one of those suffixes).
<pre><code><span>write_csv</span><span>(</span><span>iris</span>, <span>&quot;iris.csv.bz2&quot;</span><span>)</span></code></pre>
</li>
<li><code>parse_factor(levels = NULL)</code> and <code>col_factor(levels = NULL)</code> will produce a factor column based on the levels in the data, mimicing factor parsing in base R (with the exception that levels are created in the order seen).
<pre><code><span>iris2</span> <span>&lt;-</span> <span>read_csv</span><span>(</span><span>&quot;iris.csv.bz2&quot;</span>, <span>col_types</span> <span>=</span> <span>cols</span><span>(</span>
  <span>Species</span> <span>=</span> <span>col_factor</span><span>(</span><span>levels</span> <span>=</span> <span>NULL</span><span>)</span>
<span>)</span><span>)</span></code></pre>
</li>
</ul>
<p>See the full list of other changes in the <a target="_blank" rel="nofollow" href="https://github.com/tidyverse/readr/releases/tag/v1.1.0">release notes</a>.</p>
</div>
<div>
<h2>tibble 1.3.0</h2>
<p>tibble has one handy new function: <code>deframe()</code> is the opposite of <code>enframe()</code>: it turns a two-column data frame into a named vector.</p>
<pre><code><span>df</span> <span>&lt;-</span> <span>tibble</span><span>(</span><span>x</span> <span>=</span> <span>c</span><span>(</span><span>&quot;a&quot;</span>, <span>&quot;b&quot;</span>, <span>&quot;c&quot;</span><span>)</span>, <span>y</span> <span>=</span> <span>1</span><span>:</span><span>3</span><span>)</span>
<span>deframe</span><span>(</span><span>df</span><span>)</span>
<span>#> a b c </span>
<span>#> 1 2 3</span></code></pre>
<p>See the full list of other changes in the <a target="_blank" rel="nofollow" href="https://github.com/tidyverse/tibble/releases/tag/v1.3%20.0">release notes</a>.</p>
</div>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rstudioblog.wordpress.com/4349/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/rstudioblog.wordpress.com/4349/" 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>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/5bGQw2aukns">