Anyway of running multiple functions on a single object?



I have a randomly generated sample of numbers. I would like to get the mean and standard deviation from this sample. I would also like to program this in a way where I don’t have to repeat myself. Here is what I could do vs. what I wish I could do. Does anybody know of a function that could do this for me?


# vs.

sample(1:6,10000,replace=T) %>%
  summarise(mean = mean(), sd = sd())




Perfect, thank you. I was trying invoke_map didn’t think to use invoke_map_dbl


Just assign the sample to a variable.

my_sample <- sample(1:6, 10000, replace = TRUE)
list(mean = mean(my_sample), sd = sd(my_sample))


I want to share another option simply because I like so much that it is possible. I do think invoke_map_dbl is a great solution.

Since you can also store functions in a list you don’t have to ‘invoke’.

f <- list(mean, sd)
purrr::map(f, ~ .(sample(1:6,10000,replace=T)))

Which equals in base R

lapply(f, function(fun) fun(sample(1:6,10000,replace=T)))