title: Putting string values in a column of lists
body: <p>I have data that looks as follows (example data at the bottom):</p>
  <pre><code># A tibble: 40 × 6
     rn    strata    lower upper   direction value        
     &lt;chr&gt; &lt;list&gt;    &lt;chr&gt; &lt;chr&gt;   &lt;chr&gt;     &lt;chr&gt;        
   1 A     &lt;dbl [6]&gt; 0     25      East      0 (replaced) 
   2 A     &lt;dbl [6]&gt; 25    100     East      3 (replaced) 
   3 A     &lt;dbl [6]&gt; 100   500     East      3            
   4 A     &lt;dbl [6]&gt; 500   1000    East      4            
   5 A     &lt;dbl [6]&gt; 1000  1000000 East      5            
   6 A     &lt;dbl [6]&gt; 0     25      North     0 (replaced) 
   7 A     &lt;dbl [6]&gt; 25    100     North     0 (replaced) 
   8 A     &lt;dbl [6]&gt; 100   500     North     1            
   9 A     &lt;dbl [6]&gt; 500   1000    North     28 (replaced)
  10 A     &lt;dbl [6]&gt; 1000  1000000 North     2            
  # … with 30 more rows
  </code></pre>
  <p>I would like to concatenate all <code>value</code> entries by <code>rn, direction, upper</code>. This can almost be done with the following code:</p>
  <pre><code>dat_in_new &lt;- dat %&gt;%
      # One line for each rn-group
      group_by(rn, upper, direction) %&gt;% 
      # Calculate the sum, not taking into account replaced values
      summarise(freq = sum(as.numeric(value), na.rm=TRUE), .groups = 'drop_last') %&gt;% 
      group_modify(~add_row(.,freq = sum(.$value))) %&gt;% 
      group_by(rn) %&gt;%
      summarise(freq = list(freq), .groups = &quot;drop&quot;)
  
  # A tibble: 2 × 2
    rn    freq      
    &lt;chr&gt; &lt;list&gt;    
  1 A     c(0, 0, 0, 0, 0, 4, 0, 3, 0, 0, 5, 2, 9, 0, 0, 0, 0, 0, 0, 0, 3, 1, 1, 0, 0)
  2 B     c(0, 0, 1, 0, 0, 13, 0, 2, 1, 0, 10, 3, 5, 0, 0, 1, 0, 1, 0, 0, 4, 0, 0, 1, 0)
  </code></pre>
  <p>This solution now has the correct sum, because the <code>replaced</code> values should not be added to the sum. However they should be added to the <code>list</code>. I have been trying to separate the two, but I cannot figure it out.</p>
  <h1>EDIT:</h1>
  <p>I thought it would maybe be possible to create another <code>value</code> column, say <code>value_string</code>, force <code>value</code> to numeric and keep <code>value_string</code> as strings, summarise both of them, get the sum from <code>value</code> and the values from <code>value_string</code>. But I can't figure out how to write the syntax.</p>
  <h1>Desired output:</h1>
  <pre><code># A tibble: 2 × 2
    rn    freq      
    &lt;chr&gt; &lt;list&gt;    
  1 A     c(&quot;0 (replaced)&quot;, &quot;0 (replaced)&quot;, ... )
  2 B     c(&quot;0 (replaced)&quot;, &quot;0 (replaced)&quot;, ... )
  </code></pre>
  <h1>Related questions:</h1>
  <p><a href="https://stackoverflow.com/questions/72142784/make-a-list-out-of-frequencies-concatenating-categories-to-that-list">Make a list out of frequencies, concatenating categories to that list</a></p>
  <p><a href="https://stackoverflow.com/questions/71986426/using-a-column-with-lists-of-values-to-specify-from-which-columns-to-create-an">Using a column, with lists of values, to specify from which columns to create another list of values</a></p>
  <h1>DATA</h1>
  <pre><code>library(dplyr)
  libraray(tidyr)
  dat &lt;- structure(list(rn = c(&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, 
  &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, 
  &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, 
  &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, 
  &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, 
  &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, 
  &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, 
  &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, 
  &quot;B&quot;, &quot;B&quot;, &quot;B&quot;), strata = list(
      c(0, 25, 100, 500, 1000, 1e+06), c(0, 25, 100, 500, 1000, 
      1e+06), c(0, 25, 100, 500, 1000, 1e+06), c(0, 25, 100, 500, 
      1000, 1e+06), c(0, 25, 100, 500, 1000, 1e+06), c(0, 25, 100, 
      500, 1000, 1e+06), c(0, 25, 100, 500, 1000, 1e+06), c(0, 
      25, 100, 500, 1000, 1e+06), c(0, 25, 100, 500, 1000, 1e+06
      ), c(0, 25, 100, 500, 1000, 1e+06), c(0, 25, 100, 500, 1000, 
      1e+06), c(0, 25, 100, 500, 1000, 1e+06), c(0, 25, 100, 500, 
      1000, 1e+06), c(0, 25, 100, 500, 1000, 1e+06), c(0, 25, 100, 
      500, 1000, 1e+06), c(0, 25, 100, 500, 1000, 1e+06), c(0, 
      25, 100, 500, 1000, 1e+06), c(0, 25, 100, 500, 1000, 1e+06
      ), c(0, 25, 100, 500, 1000, 1e+06), c(0, 25, 100, 500, 1000, 
      1e+06), c(0, 25, 100, 500, 1000, 1e+06), c(0, 25, 100, 500, 
      1000, 1e+06), c(0, 25, 100, 500, 1000, 1e+06), c(0, 25, 100, 
      500, 1000, 1e+06), c(0, 25, 100, 500, 1000, 1e+06), c(0, 
      25, 100, 500, 1000, 1e+06), c(0, 25, 100, 500, 1000, 1e+06
      ), c(0, 25, 100, 500, 1000, 1e+06), c(0, 25, 100, 500, 1000, 
      1e+06), c(0, 25, 100, 500, 1000, 1e+06), c(0, 25, 100, 500, 
      1000, 1e+06), c(0, 25, 100, 500, 1000, 1e+06), c(0, 25, 100, 
      500, 1000, 1e+06), c(0, 25, 100, 500, 1000, 1e+06), c(0, 
      25, 100, 500, 1000, 1e+06), c(0, 25, 100, 500, 1000, 1e+06
      ), c(0, 25, 100, 500, 1000, 1e+06), c(0, 25, 100, 500, 1000, 
      1e+06), c(0, 25, 100, 500, 1000, 1e+06), c(0, 25, 100, 500, 
      1000, 1e+06)), lower = c(&quot;0&quot;, &quot;25&quot;, &quot;100&quot;, &quot;500&quot;, &quot;1000&quot;, 
  &quot;0&quot;, &quot;25&quot;, &quot;100&quot;, &quot;500&quot;, &quot;1000&quot;, &quot;0&quot;, &quot;25&quot;, &quot;100&quot;, &quot;500&quot;, &quot;1000&quot;, 
  &quot;0&quot;, &quot;25&quot;, &quot;100&quot;, &quot;500&quot;, &quot;1000&quot;, &quot;0&quot;, &quot;25&quot;, &quot;100&quot;, &quot;500&quot;, &quot;1000&quot;, 
  &quot;0&quot;, &quot;25&quot;, &quot;100&quot;, &quot;500&quot;, &quot;1000&quot;, &quot;0&quot;, &quot;25&quot;, &quot;100&quot;, &quot;500&quot;, &quot;1000&quot;, 
  &quot;0&quot;, &quot;25&quot;, &quot;100&quot;, &quot;500&quot;, &quot;1000&quot;), upper = c(&quot;25&quot;, &quot;100&quot;, &quot;500&quot;, 
  &quot;1000&quot;, &quot;1000000&quot;, &quot;25&quot;, &quot;100&quot;, &quot;500&quot;, &quot;1000&quot;, &quot;1000000&quot;, &quot;25&quot;, 
  &quot;100&quot;, &quot;500&quot;, &quot;1000&quot;, &quot;1000000&quot;, &quot;25&quot;, &quot;100&quot;, &quot;500&quot;, &quot;1000&quot;, 
  &quot;1000000&quot;, &quot;25&quot;, &quot;100&quot;, &quot;500&quot;, &quot;1000&quot;, &quot;1000000&quot;, &quot;25&quot;, &quot;100&quot;, 
  &quot;500&quot;, &quot;1000&quot;, &quot;1000000&quot;, &quot;25&quot;, &quot;100&quot;, &quot;500&quot;, &quot;1000&quot;, &quot;1000000&quot;, 
  &quot;25&quot;, &quot;100&quot;, &quot;500&quot;, &quot;1000&quot;, &quot;1000000&quot;), direction = c(&quot;East&quot;, 
  &quot;East&quot;, &quot;East&quot;, &quot;East&quot;, &quot;East&quot;, &quot;North&quot;, &quot;North&quot;, &quot;North&quot;, &quot;North&quot;, 
  &quot;North&quot;, &quot;South&quot;, &quot;South&quot;, &quot;South&quot;, &quot;South&quot;, &quot;South&quot;, &quot;West&quot;, 
  &quot;West&quot;, &quot;West&quot;, &quot;West&quot;, &quot;West&quot;, &quot;East&quot;, &quot;East&quot;, &quot;East&quot;, &quot;East&quot;, 
  &quot;East&quot;, &quot;North&quot;, &quot;North&quot;, &quot;North&quot;, &quot;North&quot;, &quot;North&quot;, &quot;South&quot;, 
  &quot;South&quot;, &quot;South&quot;, &quot;South&quot;, &quot;South&quot;, &quot;West&quot;, &quot;West&quot;, &quot;West&quot;, &quot;West&quot;, 
  &quot;West&quot;), value = c(&quot;0 (replaced)&quot;, &quot;3 (replaced)&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, 
  &quot;0 (replaced)&quot;, &quot;0 (replaced)&quot;, &quot;1&quot;, &quot;28 (replaced)&quot;, &quot;2&quot;, &quot;0 (replaced)&quot;, 
  &quot;2 (replaced)&quot;, &quot;1&quot;, &quot;3&quot;, &quot;9&quot;, &quot;0 (replaced)&quot;, &quot;1 (replaced)&quot;, &quot;9 (replaced)&quot;, 
  &quot;8 (replaced)&quot;, &quot;21 (replaced)&quot;, &quot;1&quot;, &quot;61 (replaced)&quot;, &quot;4&quot;, &quot;13&quot;, &quot;10&quot;, 
  &quot;2 (replaced)&quot;, &quot;12 (replaced)&quot;, &quot;48 (replaced)&quot;, &quot;32 (replaced)&quot;, &quot;3&quot;, 
  &quot;1&quot;, &quot;1&quot;, &quot;76 (replaced)&quot;, &quot;2&quot;, &quot;5&quot;, &quot;0 (replaced)&quot;, &quot;4 (replaced)&quot;, 
  &quot;1&quot;, &quot;1&quot;, &quot;15 (replaced)&quot;)), row.names = c(NA, -40L), class = c(&quot;tbl_df&quot;, 
  &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>