r - How to write a function with same interface as dplyr::filter but which is doing something different -
i implement function has same interface filter method in dplyr instead of removing rows not matching condition would, instance, return array indicator variable, or attach such column returned tibble?
i find useful since allow me compute summaries of columns after , before filtering summaries of rows have been removed on single tibble.
i find dplyr::filter interface convenient , therefore emulate it.
i think group_by here
you might filter summarise so
library(dplyr) mtcars %>% filter(cyl==4) %>% summarise(mean=mean(gear)) # mean # 1 4.090909 you can group_by, summarise, filter
mtcars %>% group_by(cyl) %>% summarise(mean=mean(gear)) # optional filter here # # tibble: 3 x 2 # cyl mean # <dbl> <dbl> # 1 4 4.090909 # 2 6 3.857143 # 3 8 3.285714 you can group conditionals well, so
mtcars %>% group_by(cyl > 4) %>% summarise(mean=mean(gear)) # # tibble: 2 x 2 # `cyl > 4` mean # <lgl> <dbl> # 1 false 4.090909 # 2 true 3.476190
Comments
Post a Comment