title: How to calculate 95% CIs out of duplicated values in a same series of computation using pipes `|>`
body: <p>I want to calculate the 95% CI of the value that is duplicated in a column. I have the following data, where the values of <code>total_rt</code> (sum of rt by participant and condition) are duplicated in a same participant and condition:</p>
  <pre class="lang-r prettyprint-override"><code>data &lt;- tribble(
    ~ participant, ~ condition, ~ region, ~ rt,
      &quot;p1&quot;,          &quot;a&quot;,         1,        26,
      &quot;p1&quot;,          &quot;a&quot;,         2,        56,
      &quot;p1&quot;,          &quot;a&quot;,         3,        43,
      &quot;p1&quot;,          &quot;b&quot;,         1,        15,
      &quot;p1&quot;,          &quot;b&quot;,         2,        42,
      &quot;p1&quot;,          &quot;b&quot;,         3,        36,
      &quot;p2&quot;,          &quot;a&quot;,         1,        37,
      &quot;p2&quot;,          &quot;a&quot;,         2,        67,
      &quot;p2&quot;,          &quot;a&quot;,         3,        37,
      &quot;p2&quot;,          &quot;b&quot;,         1,        7,
      &quot;p2&quot;,          &quot;b&quot;,         2,        57,
      &quot;p2&quot;,          &quot;b&quot;,         3,        27
  ) |&gt;
    group_by(
      participant,
      condition
    ) |&gt;
    mutate(
      total_rt = sum(rt)
    ) |&gt;
    ungroup()
  
  # A tibble: 12 × 5
     participant condition region    rt total_rt
     &lt;chr&gt;       &lt;chr&gt;      &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;
   1 p1          a              1    26      125
   2 p1          a              2    56      125
   3 p1          a              3    43      125
   4 p1          b              1    15       93
   5 p1          b              2    42       93
   6 p1          b              3    36       93
   7 p2          a              1    37      141
   8 p2          a              2    67      141
   9 p2          a              3    37      141
  10 p2          b              1     7       91
  11 p2          b              2    57       91
  12 p2          b              3    27       91
  </code></pre>
  <p>I want to add the columns for the mean and 95% CI of <code>total_rt</code> as shown below. I want to do so by adding sequence of data manipulation procedures using pipes <code>|&gt;</code>, so that I can avoid creating a new data frame only for the calculation of the summary statistics of <code>total_rt</code>:</p>
  <pre><code># A tibble: 12 × 8
     participant condition region    rt total_rt mean_total_rt upper_total_rt lower_total_rt
     &lt;chr&gt;       &lt;chr&gt;      &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;         &lt;dbl&gt;          &lt;dbl&gt;         &lt;dbl&gt;
   1 p1          a              1    26      125           133          149.          117.
   2 p1          a              2    56      125           133          149.          117.
   3 p1          a              3    43      125           133          149.          117.
   4 p1          b              1    15       93            92           94.0          90.
   5 p1          b              2    42       93            92           94.0          90.
   6 p1          b              3    36       93            92           94.0          90.
   7 p2          a              1    37      141           133          149.          117.
   8 p2          a              2    67      141           133          149.          117.
   9 p2          a              3    37      141           133          149.          117.
  10 p2          b              1     7       91            92           94.0          90.
  11 p2          b              2    57       91            92           94.0          90.
  12 p2          b              3    27       91            92           94.0          90.
  </code></pre>
  <p>To obtain the result above, I currently have to provide a new data frame, called <code>newdata</code> here, for the computation of summary statistics using <code>distinct()</code>, and attach it to the original data frame <code>data</code> using <code>left_join()</code>, as shown below. I want to replace these procedures with some sequence of data manipulation that directly follows the procedure to create <code>data</code> (i.e. follows <code>... |&gt; ungroup()</code> in the example above). Does anybody have a way to achieve this?</p>
  <pre><code>newdata &lt;- data |&gt;
    group_by(condition) |&gt;
    distinct(
      .keep_all = TRUE,
      participant
    ) |&gt;
    mutate(
      mean_total_rt = mean(total_rt),
      sd_total_rt = sd(total_rt),
      upper_total_rt = mean_total_rt + qnorm(0.975) * sd(total_rt) / sqrt(n()),
      lower_total_rt = mean_total_rt - qnorm(0.975) * sd(total_rt) / sqrt(n())
    ) |&gt;
    ungroup()
  
  Data &lt;- left_join(
    data,
    newdata,
    by = c(&quot;participant&quot;, &quot;condition&quot;)
  )
  
  Data |&gt; View()
  </code></pre>
  <p>Note that, the calculation of 95% CIs (and standard deviation to calculate 95% CIs) fails if I avoid using <code>distinct()</code>, since <code>n()</code> wrongly counts the duplicated values.</p>
  <pre><code>data |&gt;
    group_by(condition) |&gt;
    mutate(
      mean_total_rt = mean(total_rt),
      false_sd_total_rt = sd(total_rt),
      false_upper_total_rt = mean_total_rt + qnorm(0.975) * sd(total_rt) / sqrt(n()),
      false_lower_total_rt = mean_total_rt - qnorm(0.975) * sd(total_rt) / sqrt(n())
    ) |&gt;
    ungroup() |&gt;
    View()
  </code></pre>