r - dplyr summarise_each() using multiple functions for different column subsets accross the same groups -
i'd use summarise_each()
apply multiple functions grouped dataset. however, rather apply each function all columns, i'd apply each function particular subsets. realize specifying each column summarise()
, have many variables.
is there alternate solution either 1) using summarise_each()
, deleting unneeded columns or 2) saving group_by()
result, performing multiple separate summarise_each()
operations , combining results?
if not clear, let me know , can try illustrate example code.
i suggest following: here apply min function 1 variable , max function other. merge grouping variable.
> by_species <- iris %>% group_by(species)
start variable want apply min function:
min_var <- by_species %>% summarise_each(funs(min), petal.width) min_var source: local data frame [3 x 2]
species petal.width (fctr) (dbl) 1 setosa 0.1 2 versicolor 1.0 3 virginica 1.4
then variable want apply max function:
max_var <- by_species %>% summarise_each(funs(max), sepal.width) max_var source: local data frame [3 x 2]
species sepal.width (fctr) (dbl) 1 setosa 4.4 2 versicolor 3.4 3 virginica 3.8
now, merge above two:
left_join(min_var,max_var) joining by: "species" source: local data frame [3 x 3]
species petal.width sepal.width (fctr) (dbl) (dbl) 1 setosa 0.1 4.4 2 versicolor 1.0 3.4 3 virginica 1.4 3.8
Comments
Post a Comment