Created
April 12, 2015 16:48
-
-
Save royseto/e284cde3821b05691d59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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