title: Pivot_wider giving me columns with lists in them
body: <p>I have a data frame with encounter ID and laboratory results of each encounter as follows:</p>
  <pre><code>library(tidyverse)
  
  #Columns with variables 
  encounter_id &lt;- c(1,1,1, 2,2,2, 3,3,3, 4,4,4)
  wbc &lt;- c(13,NA,13, NA,NA,14, 15,9,15, 11,10,12)
  hb &lt;- c(13,NA,12, NA,13,14, 15,NA,15, 11,8,12)
  temp &lt;- c(100,NA,97, 103,NA,104, 100,99,NA, 100,101,101)
  hr &lt;- c(133,NA,132, NA,NA,104, 155,160,NA, 60,NA,70)
  within_24_hours &lt;- c(1,0,1, 1,1,0, 1,1,1, 0,0,1)
  
  #create a tibble from above mentioned variables 
  df &lt;- tibble(encounter_id, wbc, hb, temp, hr, within_24_hours)
  </code></pre>
  <p><strong>I want to find the mean, median, max, and min of each variable in each encounter; for example, I want to find out the mean number of the &quot;WBC&quot; column in encounters 1, 2, 3, and so on for each variable, IF they were within 24 hours (wihtin_24_hours variable ==1)</strong></p>
  <p>My first thought process is to use pivot wider, drop the variables that are not within 24 hours, then aggregate results as follows:</p>
  <pre><code> df_wider &lt;- df %&gt;% pivot_wider(
     id_cols = encounter_id,
     names_from = within_24_hours,
     values_from = c(2:5)
   )
  </code></pre>
  <p>but I could not, as I keep getting a list for each variable instead of the number.</p>
  <pre><code>  encounter_id wbc_1     wbc_0     hb_1      hb_0      temp_1    temp_0    hr_1      
   hr_0   
           &lt;dbl&gt; &lt;list&gt;    &lt;list&gt;    &lt;list&gt;    &lt;list&gt;    &lt;list&gt;    &lt;list&gt;    &lt;list&gt;    
  &lt;list&gt; 
  1            1 &lt;dbl [2]&gt; &lt;dbl [1]&gt; &lt;dbl [2]&gt; &lt;dbl [1]&gt; &lt;dbl [2]&gt; &lt;dbl [1]&gt; &lt;dbl [2]&gt; 
  &lt;dbl [~
  2            2 &lt;dbl [2]&gt; &lt;dbl [1]&gt; &lt;dbl [2]&gt; &lt;dbl [1]&gt; &lt;dbl [2]&gt; &lt;dbl [1]&gt; &lt;dbl [2]&gt; 
  &lt;dbl [~
  3            3 &lt;dbl [3]&gt; &lt;NULL&gt;    &lt;dbl [3]&gt; &lt;NULL&gt;    &lt;dbl [3]&gt; &lt;NULL&gt;    &lt;dbl [3]&gt; 
  &lt;NULL&gt; 
  4            4 &lt;dbl [1]&gt; &lt;dbl [2]&gt; &lt;dbl [1]&gt; &lt;dbl [2]&gt; &lt;dbl [1]&gt; &lt;dbl [2]&gt; &lt;dbl [1]&gt; 
  &lt;dbl [~
  </code></pre>
  <p>Any ideas on how to proceed?</p>