Created
June 22, 2020 23:30
-
-
Save MilesMcBain/c63583a29ea3bff298b75fd3588b2a58 to your computer and use it in GitHub Desktop.
weird summarise
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
``` r | |
library(tidyverse) | |
fruits <- tribble(~fruit, ~date, ~sold, | |
"Apple", "2020-06-23", 1, | |
"Apple", "2020-06-24", 2) %>% | |
mutate(fruit = fct_expand(fruit, "Banana")) | |
levels(fruits$fruit) | |
#> [1] "Apple" "Banana" | |
fruits %>% | |
group_by(fruit, .drop = FALSE) %>% | |
summarise(weird = sum(sold) / 1) | |
#> `summarise()` ungrouping output (override with `.groups` argument) | |
#> # A tibble: 2 x 2 | |
#> fruit weird | |
#> <fct> <dbl> | |
#> 1 Apple 3 | |
#> 2 Banana 0 | |
fruits %>% | |
group_by(fruit, .drop = FALSE) %>% | |
summarise(weird = 1 / sum(sold)) | |
#> `summarise()` ungrouping output (override with `.groups` argument) | |
#> # A tibble: 2 x 2 | |
#> fruit weird | |
#> <fct> <dbl> | |
#> 1 Apple 0.333 | |
#> 2 Banana Inf | |
fruits %>% | |
group_by(fruit, .drop = FALSE) %>% | |
summarise(weird = max(sold)) | |
#> Warning in max(sold): no non-missing arguments to max; returning -Inf | |
#> `summarise()` ungrouping output (override with `.groups` argument) | |
#> # A tibble: 2 x 2 | |
#> fruit weird | |
#> <fct> <dbl> | |
#> 1 Apple 2 | |
#> 2 Banana -Inf | |
``` | |
<sup>Created on 2020-06-23 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment