dplyr summarize by string
<p>I have a dataframe that has numeric and string values, for example:</p>

<pre><code> mydf &lt;- data.frame(id = c(1, 2, 1, 2, 3, 4),
               value = c(32, 12, 43, 6, 50, 20),
               text = c('A', 'B', 'A', 'B', 'C', 'D'))
</code></pre>

<p>The value of <code>id</code> variable always corresponds to <code>text</code> variable, e.g., <code>id == 1</code> will always be <code>text == 'A'</code>.</p>

<p>Now, I want to summarize this dataframe by <code>id</code> (or by <code>text</code>, since it's the same thing):</p>

<pre><code>mydf %&gt;%
  group_by(id) %&gt;%
  summarize(mean_value = mean(value))
</code></pre>

<p>This works nicely, but I also need the <code>text</code> variable, since I wan t to do text analysis. </p>

<p>However, when I add <code>text</code> to the dplyr pipe:</p>

<pre><code>mydf %&gt;%
  group_by(id) %&gt;%
  summarize(mean_value = mean(value),
  text = text)
</code></pre>

<p>I get the following error: </p>

<blockquote>
  <p>Error: expecting a single value</p>
</blockquote>

<p>Since <code>text</code> for <code>id</code> is always the same, is it possible to append it to the summarized dataframe?</p>