I would like to know if there is a way to achieve multiple outputs from a singular input using the magrittr
package in R in such a way that you could achieve the output of this:
rnorm(30) %>%
mean
rnorm(30) %>%
median
without needing to call rnorm(30)
twice.
Just put them in a vector together:
rnorm(30) %>% { c(mean = mean(.), median = median(.)) }
# mean median
# -0.2477345 -0.1126395
The curly brackets are the key here. Without them you'll get the rnorm(30)
vector with the mean and median values concatenated to the end.
Another option is to write your own function.
f <- function(x) c(mean = mean(x), median = median(x))
rnorm(30) %>% f
# mean median
# -0.12908354 -0.06667819