Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R dplyr: Write list output to dataframe

Suppose I have the following function

SlowFunction = function(vector){
  return(list(
    mean =mean(vector),
    sd  = sd(vector)
    ))
  }

And I would like to use dplyr:summarise to write the results to a dataframe:

iris %>% 
  dplyr::group_by(Species) %>% 
  dplyr::summarise(
    mean = SlowFunction(Sepal.Length)$mean,
    sd   = SlowFunction(Sepal.Length)$sd
    )

Does anyone have a suggestion how I can do this by calling "SlowFunction" once instead of twice? (In my code "SlowFunction" is a slow function that I have to call many times.) Without splitting "SlowFunction" in two parts of course. So actually I would like to somehow fill multiple columns of a dataframe in one statement.

like image 527
Frank Avatar asked Dec 11 '22 03:12

Frank


1 Answers

Without changing your current SlowFunction one way is to use do

library(dplyr)

iris %>% 
   group_by(Species) %>% 
   do(data.frame(SlowFunction(.$Sepal.Length)))

#  Species     mean    sd
#  <fct>      <dbl> <dbl>
#1 setosa      5.01 0.352
#2 versicolor  5.94 0.516
#3 virginica   6.59 0.636

Or with group_split + purrr::map_dfr

bind_cols(Species = unique(iris$Species), iris %>%
     group_split(Species) %>%
     map_dfr(~SlowFunction(.$Sepal.Length)))
like image 120
Ronak Shah Avatar answered Jan 03 '23 10:01

Ronak Shah