<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/seplyr-0-5-8-now-available-on-cran/"></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/07/seplyr-0-5-8-now-available-on-cran/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>We are pleased to announce that <a rel="nofollow" href="https://winvector.github.io/seplyr/" target="_blank">seplyr</a> version 0.5.8 is now <a rel="nofollow" href="https://cran.r-project.org/package=seplyr" target="_blank">available on CRAN</a>.</p>
<p>seplyr is an <a rel="nofollow" href="https://www.r-project.org/" target="_blank">R</a> package that provides a thin wrapper around elements of the <a rel="nofollow" href="https://cran.r-project.org/package=dplyr" target="_blank">dplyr</a> package and (now with version 0.5.8) the <a rel="nofollow" href="https://cran.r-project.org/package=tidyr" target="_blank">tidyr</a> package. The intent is to give <a rel="nofollow" href="http://www.win-vector.com/blog/2017/08/lets-have-some-sympathy-for-the-part-time-r-user/" target="_blank">the part time R user</a> the ability to easily program over functions from the popular <a rel="nofollow" href="https://cran.r-project.org/package=dplyr" target="_blank">dplyr</a> and <a rel="nofollow" href="https://cran.r-project.org/package=tidyr" target="_blank">tidyr</a> packages. Our assumption is always that a data scientist most often comes to R to work with data, not to tinker with the programming language itself.</p>
<p><span></span></p>
<p>
<p>Tools such as <a rel="nofollow" href="https://winvector.github.io/seplyr/" target="_blank">seplyr</a>, <a rel="nofollow" href="https://winvector.github.io/wrapr/" target="_blank">wrapr</a> or <a rel="nofollow" href="https://cran.r-project.org/package=rlang" target="_blank">rlang</a> are needed when you (the data scientist temporarily working on a programming sub-task) do not know the names of the columns you want your code to be working with. These are situations where you expect the column names to be made available later, in additional variables or parameters.</p>
<p>For an example: suppose we have following data where for two rows (identified by the “id” column) we have two measurements each (identified by the column names “measurement1” and “measurement2”).</p>
<pre>
library(&quot;wrapr&quot;)

d &lt;- build_frame(
   'id', 'measurement1', 'measurement2' |
   1   , 'a'           , 10             |
   2   , 'b'           , 20             )

print(d)

#   id measurement1 measurement2
# 1  1            a           10
# 2  2            b           20
</pre>
<p>Further suppose we wished to have each measurement in its own row (which is often required, such as when using the <a rel="nofollow" href="https://cran.r-project.org/package=ggplot2" target="_blank">ggplot2</a> package to produce plots). In this case we need a tool to convert the data format. If we are doing this as part of an ad-hoc analysis (i.e. we can look at the data and find the column names at the time of coding) we can use tidyr to perform the conversion:</p>
<pre>
library(&quot;tidyr&quot;)

gather(d,
       key = value_came_from_column,
       value = value_was,
       measurement1, measurement2)

#   id value_came_from_column value_was
# 1  1           measurement1         a
# 2  2           measurement1         b
# 3  1           measurement2        10
# 4  2           measurement2        20
</pre>
<p>Notice, however, all column names are specified in gather() without quotes. The names are taken from unexecuted versions of the actual source code of the arguments to gather(). This is somewhat convenient for the analyst (they can skip writing a few quote marks), but a severe limitation imposed on the script writer or programmer (they have problems taking the names of columns from other sources).</p>
<p>seplyr now supplies a standard value oriented interface for gather(). With seplyr we can write code such as the following:</p>
<pre>
library(&quot;seplyr&quot;)

gather_se(d,
  key = &quot;value_came_from_column&quot;,
  value = &quot;value_was&quot;,
  columns = c(&quot;measurement1&quot;, &quot;measurement2&quot;))
  
#   id value_came_from_column value_was
# 1  1           measurement1         a
# 2  2           measurement1         b
# 3  1           measurement2        10
# 4  2           measurement2        20
</pre>
<p>This sort of interface is handy when the names of the columns are coming from elsewhere, in variables. Here is an example of that situation:</p>
<pre>
# pretend these assignments are done elsewhere
# by somebody else
key_col_name &lt;- &quot;value_came_from_column&quot;
value_col_name &lt;- &quot;value_was&quot;
value_columns &lt;- c(&quot;measurement1&quot;, &quot;measurement2&quot;)

# we can use the above values with
# code such as this
gather_se(d,
          key = key_col_name,
          value = value_col_name,
          columns = value_columns)

#   id value_came_from_column value_was
# 1  1           measurement1         a
# 2  2           measurement1         b
# 3  1           measurement2        10
# 4  2           measurement2        20
</pre>
<p>There are ways to use gather() with “to be named later” column names directly, but it is not simple as it neeedlessly forces the user to master a number of internal implementation details of rlang and dplyr. From documentation and “help(gather)” we can deduce at least 3 related “pure tidyeval/rlang” programming over gather() solutions:</p>
<pre>
# possibly the solution hinted at in help(gather)
gather(d,
       key = !!key_col_name,
       value = !!value_col_name,
       dplyr::one_of(value_columns))

# concise rlang solution
gather(d,
       key = !!key_col_name,
       value = !!value_col_name,
       !!!value_columns)

# fully qualified rlang solution
gather(d,
       key = !!rlang::sym(key_col_name),
       value = !!rlang::sym(value_col_name),
       !!!rlang::syms(value_columns))
</pre>
<p>In all cases the user must prepare and convert values for use. Really this is showing gather() does not conveniently expect parametric columns (column names supplied by variables or parameters), but will accept a work-around if the user re-codes column names in some way (some combination of quoting and de-quoting). With “gather_se()” the tool expects to take values and the user does not have to make special arrangements (or remember special notation) to supply them.</p>
<p>Our advice for analysts is:</p>
<ul>
<li>If your goal is to work with data: use a combination of <a rel="nofollow" href="https://cran.r-project.org/web/packages/wrapr/vignettes/let.html" target="_blank">wrapr::let()</a> (a preferred user friendly solution that in fact pre-dates rlang) and <a rel="nofollow" href="https://cran.r-project.org/package=seplyr" target="_blank">seplyr</a> (a data-friendly wrapper over dplyr and tidyr functions).</li>
<li>If your goal is to write an article about rlang: then use rlang. </li>
<li>If you are interested in more advanced data manipulation, please check out our <a rel="nofollow" href="https://cran.r-project.org/package=cdata" target="_blank">cdata</a> package (video introduction <a rel="nofollow" href="https://youtu.be/4cYbP3kbc0k" target="_blank">here</a>). The cdata equivilant<br>
of the above transform is.
<pre>
library(&quot;cdata&quot;)

control_table &lt;- build_unpivot_control(
  nameForNewKeyColumn = key_col_name,
  nameForNewValueColumn = value_col_name,
  columnsToTakeFrom = value_columns)

rowrecs_to_blocks(d, 
                  control_table, 
                  columnsToCopy = &quot;id&quot;)
#   id value_came_from_column value_was
# 1  1           measurement1         a
# 2  1           measurement2        10
# 3  2           measurement1         b
# 4  2           measurement2        20
</pre>
</li>
<li>If you need high in-memory performance: try <a rel="nofollow" href="https://github.com/Rdatatable/data.table/wiki" target="_blank">data.table</a>.</li>
</ul>
<p>In addition to wrapping a number of dplyr functions and tidyr::gather()/tidyr::spread(), seplyr 0.5.8 now also wraps tidyr::complete() (thanks to a contribution from Richard Layton).</p>
<p>We hope you try seplyr out both in your work and in your teaching.</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/07/seplyr-0-5-8-now-available-on-cran/"> 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/8xbmjim2opk" height="1"></a></div>