<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>General-purpose reshape and aggregation function for mousetrap data. — mt_reshape • mousetrap</title><!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script><!-- Bootstrap --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha256-bZLfwXAP04zRMK2BjiO8iu9pf4FbLqX6zitd+tIvLhE=" crossorigin="anonymous"><script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="../bootstrap-toc.css"><script src="../bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous"><!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="../pkgdown.css" rel="stylesheet"><script src="../pkgdown.js"></script><meta property="og:title" content="General-purpose reshape and aggregation function for mousetrap data. — mt_reshape"><meta property="og:description" content='mt_reshape is the general function used in the mousetrap
package for filtering, merging, reshaping, and aggregating mouse-tracking
measures or trajectories in combination with other trial data. Several
additional (wrapper) functions for more specific purposes (cf. "See Also")
are available.'><!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]--></head><body data-spy="scroll" data-target="#toc">
    

    <div class="container template-reference-topic">
      <header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="../index.html">mousetrap</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="">3.2.3</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav"><li>
  <a href="../reference/mousetrap-package.html">Overview</a>
</li>
<li>
  <a href="../reference/index.html">Reference</a>
</li>
<li>
  <a href="../news/index.html">News</a>
</li>
      </ul><ul class="nav navbar-nav navbar-right"><li>
  <a href="https://github.com/pascalkieslich/mousetrap" class="external-link">
    <span class="fa fa-github fa-lg"></span>
     
  </a>
</li>
      </ul></div><!--/.nav-collapse -->
  </div><!--/.container -->
</div><!--/.navbar -->

      

      </header><div class="row">
  <div class="col-md-9 contents">
    <div class="page-header">
    <h1>General-purpose reshape and aggregation function for mousetrap data.</h1>
    <small class="dont-index">Source: <a href="https://github.com/pascalkieslich/mousetrap/blob/HEAD/R/reshape.R" class="external-link"><code>R/reshape.R</code></a></small>
    <div class="hidden name"><code>mt_reshape.Rd</code></div>
    </div>

    <div class="ref-description">
    <p><code>mt_reshape</code> is the general function used in the <code>mousetrap</code>
package for filtering, merging, reshaping, and aggregating mouse-tracking
measures or trajectories in combination with other trial data. Several
additional (wrapper) functions for more specific purposes (cf. "See Also")
are available.</p>
    </div>

    <div id="ref-usage">
    <div class="sourceCode"><pre class="sourceCode r"><code><span><span class="fu">mt_reshape</span><span class="op">(</span></span>
<span>  <span class="va">data</span>,</span>
<span>  use <span class="op">=</span> <span class="st">"trajectories"</span>,</span>
<span>  use_variables <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  use2 <span class="op">=</span> <span class="st">"data"</span>,</span>
<span>  use2_variables <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  subset <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  subject_id <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  aggregate <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span>  aggregate_subjects_only <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span>  .funs <span class="op">=</span> <span class="st">"mean"</span>,</span>
<span>  trajectories_long <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>  convert_df <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>  mt_id <span class="op">=</span> <span class="st">"mt_id"</span>,</span>
<span>  mt_seq <span class="op">=</span> <span class="st">"mt_seq"</span>,</span>
<span>  aggregation_function <span class="op">=</span> <span class="cn">NULL</span></span>
<span><span class="op">)</span></span></code></pre></div>
    </div>

    <div id="arguments">
    <h2>Arguments</h2>
    <dl><dt>data</dt>
<dd><p>a mousetrap data object created using one of the mt_import
functions (see <a href="mt_example.html">mt_example</a> for details). Alternatively, a trajectory
array can be provided directly (in this case <code>use</code> will be ignored).</p></dd>


<dt>use</dt>
<dd><p>a character string specifying which data should be reshaped. The
corresponding data are selected from data using <code>data[[use]]</code>.
Usually, this value corresponds to either "trajectories",
"tn_trajectories", or "measures", depending on whether the analysis
concerns raw trajectories, time-normalized trajectories, or derived
measures.</p></dd>


<dt>use_variables</dt>
<dd><p>a character vector specifying which mouse-tracking
variables should be reshaped. Corresponds to the column names in case a
data.frame with mouse-tracking measures is provided. Corresponds to the
labels of the array dimensions in case a trajectory array is provided. If
unspecified, all variables will be reshaped.</p></dd>


<dt>use2</dt>
<dd><p>an optional character string specifying where the other trial
data can be found. Defaults to "data" as <code>data[["data"]]</code> usually
contains all non mouse-tracking trial data. Alternatively, a data.frame can
be provided directly.</p></dd>


<dt>use2_variables</dt>
<dd><p>an optional character string (or vector) specifying the
variables (in <code>data[[use2]]</code>) that should be merged with the data. If
<code>aggregate==TRUE</code>, the trajectories / measures will be aggregated
separately for each of the levels of these variables using
<a href="https://dplyr.tidyverse.org/reference/summarise_all.html" class="external-link">summarize_at</a>.</p></dd>


<dt>subset</dt>
<dd><p>a logical expression (passed on to <a href="https://rdrr.io/r/base/subset.html" class="external-link">subset</a>) indicating
elements or rows to keep. If specified, <code>data[[use2]]</code> will be
subsetted using this expression, and, afterwards, <code>data[[use]]</code> will
be filtered accordingly.</p></dd>


<dt>subject_id</dt>
<dd><p>an optional character string specifying which column
contains the subject identifier in <code>data[[use2]]</code>. If specified and
<code>aggregate==TRUE</code>, aggregation will be performed within subjects
first.</p></dd>


<dt>aggregate</dt>
<dd><p>logical indicating whether data should be aggregated. If
<code>use2_variables</code> are specified, aggregation will be performed
separately for each of the levels of the <code>use2_variables</code>.</p></dd>


<dt>aggregate_subjects_only</dt>
<dd><p>logical indicating whether data should only be
aggregated per subject (if <code>subject_id</code> is specified and
<code>aggregate==TRUE</code>).</p></dd>


<dt>.funs</dt>
<dd><p>the aggregation function(s) passed on to
<code><a href="https://dplyr.tidyverse.org/reference/summarise_all.html" class="external-link">summarize_at</a></code>. By default, the <code><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean</a></code> is
calculated.</p></dd>


<dt>trajectories_long</dt>
<dd><p>logical indicating if the reshaped trajectories
should be returned in long or wide format. If <code>TRUE</code>, every recorded
position in a trajectory is placed in another row (whereby the order of the
positions is logged in the variable <code>mt_seq</code>). If <code>FALSE</code>, every
trajectory is saved in wide format and the respective positions are indexed
by adding an integer to the corresponding label (e.g., <code>xpos_1</code>,
<code>xpos_2</code>, ...). Only relevant if <code>data[[use]]</code> contains
trajectories.</p></dd>


<dt>convert_df</dt>
<dd><p>logical indicating if the reshaped data should be converted
to a <code><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></code> using <code><a href="https://rdrr.io/r/base/as.data.frame.html" class="external-link">as.data.frame</a></code>. This will
drop potentially existing additional classes (such as
<code><a href="https://dplyr.tidyverse.org/reference/tbl_df.html" class="external-link">tbl_df</a></code>) that result from the internally used
<code>dplyr</code> functions for data grouping and aggregation. As these
additional classes might - on rare occasions - cause problems with
functions from other packages, the reshaped data are converted to "pure"
data.frames by default.</p></dd>


<dt>mt_id</dt>
<dd><p>a character string specifying the name of the column that will
contain the trial identifier in the reshaped data. The values for the trial
identifier correspond to the <code>rownames</code> of <code>data[[use]]</code> and
<code>data[[use2]]</code>.</p></dd>


<dt>mt_seq</dt>
<dd><p>a character string specifying the name of the column that will
contain the integers indicating the order of the mouse positions per
trajectory in the reshaped data. Only relevant if <code>data[[use]]</code>
contains trajectories and <code>trajectories_long==TRUE</code>.</p></dd>


<dt>aggregation_function</dt>
<dd><p>Deprecated. Please use <code>.funs</code> instead.</p></dd>

</dl></div>
    <div id="value">
    <h2>Value</h2>
    

<p>A <code><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></code> containing the reshaped data.</p>
    </div>
    <div id="details">
    <h2>Details</h2>
    <p><code>mt_reshape</code> uses the <a href="https://rdrr.io/r/base/colnames.html" class="external-link">rownames</a> of <code>data[[use]]</code> and
<code>data[[use2]]</code> for merging the trajectories / measures and the trial
data. For convenience (and for trajectories in long format also of
necessity), an additional column (labelled as specified in the <code>mt_id</code>
argument) is added to the reshaped data containing the rownames as trial
identifier.</p>
<p>The main purpose of this function is to reshape the trajectory data into a
two-dimensional data.frame, as this format is required for many further
analyses and plots in R.</p>
<p>Besides, it should aid the user in combining data contained in different
parts of the mousetrap data object, e.g., a condition variable stored in
<code>data[["data"]]</code> with trajectory data stored in
<code>data[["trajectories"]]</code> (or mouse-tracking measures stored in
<code>data[["measures"]]</code>).</p>
<p>Finally, it offers the possibility to aggregate trajectories and measures for
different conditions and/or subjects.</p>
<p>The package also includes several functions that wrap <code>mt_reshape</code> and
serve specific purposes. They are often easier to use, and thus recommended
over <code>mt_reshape</code> unless the utmost flexibility is required. These
functions are described in the section "See Also".</p>
<p>Note also that many merging, reshaping, and aggregation procedures can be
performed directly by using some of the basic R functions, e.g., <a href="https://rdrr.io/r/base/merge.html" class="external-link">merge</a>
and <a href="https://rdrr.io/r/stats/aggregate.html" class="external-link">aggregate</a>, or through the R packages <code>dplyr</code> or
<code>reshape2</code>, if desired.</p>
    </div>
    <div id="see-also">
    <h2>See also</h2>
    <div class="dont-index"><p><a href="mt_aggregate.html">mt_aggregate</a> for aggregating mouse-tracking measures and
trajectories.</p>
<p><a href="mt_aggregate_per_subject.html">mt_aggregate_per_subject</a> for aggregating mouse-tracking measures and
trajectories per subject.</p>
<p><a href="mt_export_long.html">mt_export_long</a> for exporting mouse-tracking data in long format.</p>
<p><a href="mt_export_long.html">mt_export_wide</a> for exporting mouse-tracking data in wide format.</p>
<p><a href="https://dplyr.tidyverse.org/reference/mutate-joins.html" class="external-link">inner_join</a> for merging data and <code><a href="https://dplyr.tidyverse.org/reference/summarise_all.html" class="external-link">summarize_at</a></code>
for aggregating data using the <code>dplyr</code> package.</p></div>
    </div>
    <div id="author">
    <h2>Author</h2>
    <p>Pascal J. Kieslich</p>
<p>Felix Henninger</p>
    </div>

    <div id="ref-examples">
    <h2>Examples</h2>
    <div class="sourceCode"><pre class="sourceCode r"><code><span class="r-in"><span><span class="co"># Time-normalize trajectories</span></span></span>
<span class="r-in"><span><span class="va">mt_example</span> <span class="op">&lt;-</span> <span class="fu"><a href="mt_time_normalize.html">mt_time_normalize</a></span><span class="op">(</span><span class="va">mt_example</span><span class="op">)</span></span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># Reshape time-normalized trajectories data into long format</span></span></span>
<span class="r-in"><span><span class="co"># adding Condition variable</span></span></span>
<span class="r-in"><span><span class="va">trajectories_long</span> <span class="op">&lt;-</span> <span class="fu">mt_reshape</span><span class="op">(</span><span class="va">mt_example</span>,</span></span>
<span class="r-in"><span> use<span class="op">=</span><span class="st">"tn_trajectories"</span>,</span></span>
<span class="r-in"><span> use2_variables<span class="op">=</span><span class="st">"Condition"</span></span></span>
<span class="r-in"><span> <span class="op">)</span></span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># Reshape time-normalized trajectories data into wide format</span></span></span>
<span class="r-in"><span><span class="co"># only keeping xpos and ypos</span></span></span>
<span class="r-in"><span><span class="co"># and adding Condition variable</span></span></span>
<span class="r-in"><span><span class="va">trajectories_wide</span> <span class="op">&lt;-</span> <span class="fu">mt_reshape</span><span class="op">(</span><span class="va">mt_example</span>,</span></span>
<span class="r-in"><span>  use<span class="op">=</span><span class="st">"tn_trajectories"</span>, use_variables <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"xpos"</span>,<span class="st">"ypos"</span><span class="op">)</span>,</span></span>
<span class="r-in"><span>  use2_variables <span class="op">=</span> <span class="st">"Condition"</span>,</span></span>
<span class="r-in"><span>  trajectories_long <span class="op">=</span> <span class="cn">FALSE</span></span></span>
<span class="r-in"><span>  <span class="op">)</span></span></span>
<span class="r-in"><span></span></span>
</code></pre></div>
    </div>
  </div>
  <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
    <nav id="toc" data-toggle="toc" class="sticky-top"><h2 data-toc-skip>Contents</h2>
    </nav></div>
</div>


      <footer><div class="copyright">
  <p></p><p>Developed by Pascal J. Kieslich, Dirk U. Wulff, Felix Henninger, Jonas M. B. Haslbeck.</p>
</div>

<div class="pkgdown">
  <p></p><p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.0.7.</p>
</div>

      </footer></div>

  


  

  </body></html>

