Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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)
                
df1 = iris %>% group_by(Species) %>% nest()


                      
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 1×3 long and an additional statistic as a third column.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

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]>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading