Search code examples
rnestedpurrrtibble

How to append a new column made of single dbl to nested tibble column?


I have a nested tibble with a column that is basically a bunch of different summary statistics for that group, in the example below it's the same as fun1_result. These were calculated with fun1.

I now want to append a new statistic to the fun1_result column by using fun2. I was guessing I should use map_dbl but I don't actually know what the exact syntax would be to create an additional column within the fun1_result column, basically appending a column to fun1_result.


library(tidyverse)
                
                     
fun1 = function(df){
  
result1 = min(df[,"Sepal.Width"])/max(df["Sepal.Length"])

result2 = min(df[,"Sepal.Length"])/max(df["Sepal.Length"])

results = tibble(result1,result2)

return(results)
  
}


fun2 = function(df){
  
result3 = min(df[,"Sepal.Width"])/2

return(result3)
  
}


df1 = iris %>% group_by(Species) %>% nest()
                         
df1 = df1 %>% mutate(fun1_result = map(.x = data,~fun1(.x)))     


The result I would be looking for would be the same as the current nested dataframe (after fun1 is applied) with tibbles 1x3 long and an additional statistic as a third column.

I'm sure it's very easy, but I just haven't been able to achieve this as when I used something like this it doesn't work df1 %>% mutate(fun1_result = map_dbl(.x = data, ~fun2(.x)))

Thank you


Solution

  • A way to do it could be to use bind_cols:

    library(purrr)
    library(dplyr)
    
    df1 %>%
      mutate(fun1_result = map(.x = data,~fun1(.x))) |>
      mutate(fun1_result = list(bind_cols(fun1_result, tibble(result3 = map(.x = data, ~fun2(.x))))))
    

    But it'll be nicer if fun2 returned a tibble like fun1:

    fun2 = function(df) {
        
        result3 = min(df[,"Sepal.Width"])/2
        
        return(tibble(result3))
        
    }
    
    df1 %>% 
      mutate(fun1_result = map(.x = data,~fun1(.x))) |> 
      mutate(fun1_result = list(bind_cols(fun1_result, map(.x = data, ~fun2(.x)))))
    

    Output:

    # A tibble: 3 × 3
    # Groups:   Species [3]
      Species    data              fun1_result     
      <fct>      <list>            <list>          
    1 setosa     <tibble [50 × 4]> <tibble [1 × 3]>
    2 versicolor <tibble [50 × 4]> <tibble [1 × 3]>
    3 virginica  <tibble [50 × 4]> <tibble [1 × 3]>