title: How to count elements in each column of a list column in data frame in R
body: <p>I have a data frame <code>df</code> where one of the column is a list column.  Each of the element of this column in dataframe is list.  So how do I include (mutate) a new column in <code>df</code> giving me a count of corresponding elements in that list column.</p>
  <p>sample data</p>
  <pre><code>df &lt;- structure(list(x1 = 1:5, 
                       x2 = list(c(&quot;a&quot;, &quot;b&quot;), c(&quot;b&quot;, &quot;c&quot;, &quot;d&quot;), c(&quot;a&quot;, &quot;b&quot;), c(&quot;a&quot;, &quot;b&quot;), c(&quot;a&quot;, &quot;b&quot;))), 
                  row.names = c(NA, -5L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  &gt; df
  # A tibble: 5 x 2
       x1 x2       
    &lt;int&gt; &lt;list&gt;   
  1     1 &lt;chr [2]&gt;
  2     2 &lt;chr [3]&gt;
  3     3 &lt;chr [2]&gt;
  4     4 &lt;chr [2]&gt;
  5     5 &lt;chr [2]&gt;
  
  </code></pre>
  <p>I have tried this but to no avail</p>
  <pre><code>df %&gt;% mutate(x3 = length(x2[[row_number()]]))
  
  Error: Problem with `mutate()` input `x3`.
  x recursive indexing failed at level 2
  
  i Input `x3` is `length(x2[[row_number()]])`.
  Run `rlang::last_error()` to see where the error occurred.
  </code></pre>
  <p>My desired out put is something like</p>
  <pre><code># A tibble: 5 x 3
       x1 x2           x3
    &lt;int&gt; &lt;list&gt;    &lt;int&gt;
  1     1 &lt;chr [2]&gt;     2
  2     2 &lt;chr [3]&gt;     3
  3     3 &lt;chr [2]&gt;     2
  4     4 &lt;chr [2]&gt;     2
  5     5 &lt;chr [2]&gt;     2
  </code></pre>