<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/general-gems-of-comperes/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.questionflow.org/2018/05/17/general-gems-of-comeres/"> QuestionFlow </a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> Examples of exported functions from comperes package that could be useful for general tasks.</p>
<div>
<h1>Prologue</h1>
<p>I am very glad to announce that my new package <a rel="nofollow" href="https://github.com/echasnovski/comperes" target="_blank">comperes</a> is <a rel="nofollow" href="https://cran.r-project.org/package=comperes" target="_blank">on CRAN</a> now. It provides tools for managing competition results in a <a rel="nofollow" href="https://cran.r-project.org/web/packages/tidyr/vignettes/tidy-data.html" target="_blank">tidy</a> manner as much as possible. For more information go to:</p>
<ul>
<li>Package <a rel="nofollow" href="https://github.com/echasnovski/comperes#comperes-manage-competition-results" target="_blank">README</a>.</li>
<li>Package <a rel="nofollow" href="https://echasnovski.github.io/comperes/articles/" target="_blank">vignettes</a>.</li>
<li>My <a rel="nofollow" href="http://www.questionflow.org/2018/05/09/harry-potter-and-competition-results-with-comperes/" target="_blank">previous post</a> for usage examples based on built-in <code>hp_survey</code> data set (results of my <a rel="nofollow" href="http://www.questionflow.org/2018/04/09/struggle-with-harry-potter-data/" target="_blank">Harry Potter Books Survey</a>).</li>
</ul>
<p>Besides tools for competition results, <code>comperes</code> offers some functions that can be useful in more general tasks. This post presents examples of their most common usage.</p>
</div>
<div>
<h1>Overview</h1>
<p>This post covers the following themes:</p>
<ul>
<li><strong>Compute vector levels</strong> with <a rel="nofollow" href="https://echasnovski.github.io/comperes/reference/levels2.html" target="_blank">levels2()</a>.</li>
<li><strong>Manage item summaries</strong> with <a rel="nofollow" href="https://echasnovski.github.io/comperes/reference/item-summary.html" target="_blank">summarise_item()</a> and <a rel="nofollow" href="https://echasnovski.github.io/comperes/reference/item-summary-join.html" target="_blank">join_item_summary()</a>.</li>
<li><strong>Convert pairwise data</strong> with <a rel="nofollow" href="https://echasnovski.github.io/comperes/reference/convert-pair-value.html" target="_blank">long_to_mat() and mat_to_long()</a>.</li>
</ul>
<p>For examples we will use a shortened version of the everlasting <code>mtcars</code> data set. We will need the following setup:</p>
<pre><code>library(comperes)
library(rlang)
# For example analysis
library(dplyr)
library(tibble)

mtcars_tbl &lt;- mtcars %>%
  rownames_to_column(var = &quot;car&quot;) %>%
  select(car, cyl, vs, carb) %>%
  as_tibble()</code></pre>
</div>
<div>
<h1>Compute vector levels</h1>
<p>We will start with the most simple function. During <code>comperes</code> development, idea about the it really helped me reason more clearly about package functional API. I am talking about <a rel="nofollow" href="https://echasnovski.github.io/comperes/reference/levels2.html" target="_blank">levels2()</a> which computes “levels” of any non-list vector.</p>
<p>It has the following logic: if <code>x</code> has <code>levels</code> attribute then return <code>levels(x)</code>; otherwise return character representation of vector’s sorted unique values. Notes about design and implementation of this function:</p>
<ul>
<li>I hesitated <em>a lot</em> about whether it should return character or same type as input vector in case <code>x</code> has no <code>levels</code>. In many practical cases there is a need in latter behavior. However, in the end I decided that type stable output (<code>levels(x)</code> always returns character vector or <code>NULL</code>) is better.</li>
<li>Conversion to character is done <strong>after</strong> sorting, which is really important when dealing with numeric vectors.</li>
</ul>
<p>This function is helpful when one needs to produce unique values in standardized manner (for example, during pairwise distance computation). Some examples:</p>
<pre><code>levels2(mtcars_tbl$cyl)
## [1] &quot;4&quot; &quot;6&quot; &quot;8&quot;

# Importance of conversion to character after sorting
tricky_vec &lt;- c(10, 1, 2, 12)
sort(as.character(tricky_vec))
## [1] &quot;1&quot;  &quot;10&quot; &quot;12&quot; &quot;2&quot;
levels2(tricky_vec)
## [1] &quot;1&quot;  &quot;2&quot;  &quot;10&quot; &quot;12&quot;</code></pre>
</div>
<div>
<h1>Manage item summaries</h1>
<p>Arguably, the most common task in data analysis is computation of group summaries. This task is conveniently done by consecutive application of <a rel="nofollow" href="https://dplyr.tidyverse.org/" target="_blank">dplyr</a>’s <code>group_by()</code>, <code>summarise()</code> and <code>ungroup()</code> (to return regular data frame and not grouped one). <code>comperes</code> offers a wrapper <a rel="nofollow" href="https://echasnovski.github.io/comperes/reference/item-summary.html" target="_blank">summarise_item()</a> for this task (which always returns <a rel="nofollow" href="https://tibble.tidyverse.org/" target="_blank">tibble</a> instead of a data frame) with additional feature of modifying column names by adding prefix (which will be handy soon):</p>
<pre><code>cyl_vs_summary &lt;- mtcars_tbl %>%
  summarise_item(
    item = c(&quot;cyl&quot;, &quot;vs&quot;),
    n = n(), mean_carb = mean(carb),
    .prefix = &quot;cyl_vs__&quot;
  )
cyl_vs_summary
## # A tibble: 5 x 4
##     cyl    vs cyl_vs__n cyl_vs__mean_carb
##   </code></pre></div>