Last active
February 10, 2019 19:36
-
-
Save vikjam/95596bbd3b9398e2cfafa841ed706f89 to your computer and use it in GitHub Desktop.
Summarize dplyr
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
Wrong mean: 5.84333333333333 | |
Right mean: 5.936 | |
The wrong mean didn't filter: 5.84333333333333 |
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
library(dplyr) | |
library(glue) | |
data(iris) | |
wrong_mean <- iris %>% | |
filter(iris$Species == "versicolor") %>% | |
summarize(avg_sepal_length = mean(iris$Sepal.Length)) | |
right_mean <- iris %>% | |
filter(Species == "versicolor") %>% | |
summarize(avg_sepal_length = mean(Sepal.Length)) | |
print(glue("Wrong mean: {wrong_mean} | |
Right mean: {right_mean}")) | |
unfiltered_mean <- mean(iris$Sepal.Length) | |
print(glue("The wrong mean didn't filter: {unfiltered_mean}")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment