Skip to content

Instantly share code, notes, and snippets.

@royseto
Created April 12, 2015 16:48
Show Gist options
  • Save royseto/e284cde3821b05691d59 to your computer and use it in GitHub Desktop.
Save royseto/e284cde3821b05691d59 to your computer and use it in GitHub Desktop.
# Experiments with R aggregates and window functions
# See http://cran.r-project.org/web/packages/dplyr/vignettes/window-functions.html
library(dplyr)
data(iris)
iris_pct <- iris %>%
mutate(length_pct = Petal.Length / sum(Petal.Length))
iris_pct2 <- iris %>%
mutate(length_pct_all = Petal.Length / sum(Petal.Length)) %>%
group_by(Species) %>%
mutate(length_pct_species = Petal.Length / sum(Petal.Length))
iris_total_all <- iris_pct %>%
summarise(total_pct_all = sum(length_pct))
iris_total_by_species <- iris_pct2 %>%
group_by(Species) %>%
summarise(total_pct_species = sum(length_pct_species))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment