<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/set_na_where-a-nonstandard-evaluation-use-case/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://tjmahr.github.io/set-na-where-nonstandard-evaluation-use-case/"> Higher Order Functions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this post, I describe a recent case where I used rlang’s<br>
<a rel="nofollow" href="http://rlang.tidyverse.org/articles/tidy-evaluation.html" target="_blank">tidy evaluation</a><br>
system to do some data-cleaning. This example is not particularly involved, but<br>
it demonstrates is a basic but powerful idea: That we can capture the<br>
expressions that a user writes, pass them around as data, and make some<br>
:dizzy: magic :sparkles: happen. This technique in R is called<br>
<a rel="nofollow" href="http://adv-r.had.co.nz/Computing-on-the-language.html" target="_blank"><em>nonstandard evaluation</em></a>.</p>
<h2>Strange eyetracking data</h2>
<p>Last week, I had to deal with a file with some eyetracking data from a<br>
sequence-learning experiment. The eyetracker records the participant’s gaze<br>
location at a rate of 60 frames per second—except for this weird file which<br>
wrote out ~80 frames each second. In this kind of data, we have one row per<br>
eyetracking sample, and each sample records a timestamp and the gaze location<br>
:eyes: on the computer screen at each timestamp. In this particular dataset, we<br>
have <em>x</em> and <em>y</em> gaze coordinates in pixels (both eyes averaged together,<br>
<code>GazeX</code> and <code>GazeY</code>) or in screen proportions (for each eye in the <code>EyeCoord</code><br>
columns.)</p>
<div>
<pre><code><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>rlang</span><span>)</span><span>
</span><span># the data is bundled with an R package I wrote
# devtools::install_github(&quot;tjmahr/fillgaze&quot;)
</span><span>
</span><span>df</span><span> </span><span>&lt;-</span><span> </span><span>system.file</span><span>(</span><span>&quot;test-gaze.csv&quot;</span><span>,</span><span> </span><span>package</span><span> </span><span>=</span><span> </span><span>&quot;fillgaze&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>readr</span><span>::</span><span>read_csv</span><span>()</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>Time</span><span> </span><span>=</span><span> </span><span>Time</span><span> </span><span>-</span><span> </span><span>min</span><span>(</span><span>Time</span><span>))</span><span> </span><span>%>%</span><span> </span><span>select</span><span>(</span><span>Time</span><span>:</span><span>REyeCoordY</span><span>)</span><span> </span><span>%>%</span><span> </span><span>round</span><span>(</span><span>3</span><span>)</span><span> </span><span>%>%</span><span> </span><span>mutate_at</span><span>(</span><span>vars</span><span>(</span><span>Time</span><span>),</span><span> </span><span>round</span><span>,</span><span> </span><span>1</span><span>)</span><span> </span><span>%>%</span><span> </span><span>mutate_at</span><span>(</span><span>vars</span><span>(</span><span>GazeX</span><span>,</span><span> </span><span>GazeY</span><span>),</span><span> </span><span>round</span><span>,</span><span> </span><span>0</span><span>)</span><span>
</span><span>df</span><span>
</span><span>#> # A tibble: 14,823 x 8
#> Time Trial GazeX GazeY LEyeCoordX LEyeCoordY REyeCoordX REyeCoordY
#> </span></code></pre></div>