I have a data frame with encounter ID and laboratory results of each encounter as follows:

```
library(tidyverse)

#Columns with variables 
encounter_id <- c(1,1,1, 2,2,2, 3,3,3, 4,4,4)
wbc <- c(13,NA,13, NA,NA,14, 15,9,15, 11,10,12)
hb <- c(13,NA,12, NA,13,14, 15,NA,15, 11,8,12)
temp <- c(100,NA,97, 103,NA,104, 100,99,NA, 100,101,101)
hr <- c(133,NA,132, NA,NA,104, 155,160,NA, 60,NA,70)
within_24_hours <- c(1,0,1, 1,1,0, 1,1,1, 0,0,1)

#create a tibble from above mentioned variables 
df <- tibble(encounter_id, wbc, hb, temp, hr, within_24_hours)

```

**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 "WBC" column in encounters 1, 2, 3, and so on for each variable, IF they were within 24 hours (wihtin\_24\_hours variable ==1)**

My first thought process is to use pivot wider, drop the variables that are not within 24 hours, then aggregate results as follows:

```
 df_wider <- df %>% pivot_wider(
   id_cols = encounter_id,
   names_from = within_24_hours,
   values_from = c(2:5)
 )

```

but I could not, as I keep getting a list for each variable instead of the number.

```
  encounter_id wbc_1     wbc_0     hb_1      hb_0      temp_1    temp_0    hr_1      
 hr_0   
         <dbl> <list>    <list>    <list>    <list>    <list>    <list>    <list>    
<list> 
1            1 <dbl [2]> <dbl [1]> <dbl [2]> <dbl [1]> <dbl [2]> <dbl [1]> <dbl [2]> 
<dbl [~
2            2 <dbl [2]> <dbl [1]> <dbl [2]> <dbl [1]> <dbl [2]> <dbl [1]> <dbl [2]> 
<dbl [~
3            3 <dbl [3]> <NULL>    <dbl [3]> <NULL>    <dbl [3]> <NULL>    <dbl [3]> 
<NULL> 
4            4 <dbl [1]> <dbl [2]> <dbl [1]> <dbl [2]> <dbl [1]> <dbl [2]> <dbl [1]> 
<dbl [~

```

Any ideas on how to proceed?