Skip to content

Instantly share code, notes, and snippets.

@MilesMcBain
Created June 22, 2020 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MilesMcBain/c63583a29ea3bff298b75fd3588b2a58 to your computer and use it in GitHub Desktop.
Save MilesMcBain/c63583a29ea3bff298b75fd3588b2a58 to your computer and use it in GitHub Desktop.
weird summarise
``` 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