r - How to apply a function to the entire table in a dplyr chain -


i have dplyr chain follows

myresults <- rawdata %>% filter(stuff) %>% mutate(stuff) 

now want apply function myfunc myresults. there way in chain or need do:

myresults <- myfunc(myresult) 

if function takes dataframe first argument, can simple add @ end.

> myfunc <- function(x) sapply(x, max) > mtcars  %>% filter(mpg > 20) %>%  myfunc()     mpg     cyl    disp      hp    drat      wt    qsec      vs         gear   33.900   6.000 258.000 113.000   4.930   3.215  22.900   1.000   1.000   5.000     carb    4.000  

it worth mention magrittr::%>% used dplyr works argument can this:

> inc <- function(x) x + 1 > 1 %>% inc(.) %>% sqrt(.) %>% log(.) [1] 0.3465736 

and useful magrittr aliases:

library(magrittr) set.seed(1) intrain <- sample(1:nrow(mtcars), 20) mtcarstest <- mtcars %>% extract(-intrain, )  summarypipe <- function(x) {print(summary(x)); x}  mtcars %>%     extract(intrain, ) %>%      # train lm     lm(mpg ~ ., .) %>%     # print summary , forward lm results     summarypipe %>%     # predict on test set     predict(newdata = mtcarstest) %>%     # print results , forward arguments     print %>%     # compute rmse     subtract(mtcarstest %>% extract2('mpg')) %>%     raise_to_power(2) %>%     mean %>%     sqrt 

it matter of taste find rather useful.

as @bondeddust mentioned in comments there 3 possible ways of passing function %>%. dot placeholder can use lhs on different position first (see lm call).


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -