# NOT RUN {
# compute mean of the every column for every value of the Species
data(iris)
by_groups(iris, "Species", mean)
# compute mean of the every numeric column
by_groups(iris %except% "Species", mean)
# compute different functions for different columns
# automatic naming
data(mtcars)
by_groups(mtcars, "cyl", "am", ~ mean(hp), ~ median(mpg))
# with custom names
by_groups(mtcars, "cyl", "am", mean_hp ~ mean(hp), median_mpg ~ median(mpg))
# 'qc' usage to avoide quotes
by_groups(mtcars, qc(cyl, am), ~ mean(hp), ~ median(mpg))
# variable substitution
group1 = "cyl"
statistic1 = as.formula("~ mean(hp)")
by_groups(mtcars, group1, statistic1)
group2 = "am"
statistic2 = as.formula("~ median(mpg)")
by_groups(mtcars, group2, statistic2)
by_groups(mtcars, group1, group2, statistic1, statistic2)
# infix version
iris %by_groups% c("Species", mean)
mtcars %by_groups% c("cyl", "am", mean_hp ~ mean(hp), median_mpg ~ median(mpg))
# }
Run the code above in your browser using DataLab