Skip to content

Instantly share code, notes, and snippets.

@apreshill
Last active July 16, 2019 13:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apreshill/d9107e054f3358d438e04a582b76f7cc to your computer and use it in GitHub Desktop.
Save apreshill/d9107e054f3358d438e04a582b76f7cc to your computer and use it in GitHub Desktop.
``` r
library(tidyverse)
iris %>%
count(Species)
#> # A tibble: 3 x 2
#> Species n
#> <fct> <int>
#> 1 setosa 50
#> 2 versicolor 50
#> 3 virginica 50
iris %>%
count(Species) %>%
count()
#> # A tibble: 1 x 1
#> n
#> <int>
#> 1 3
# can also name it
iris %>%
count(Species) %>%
count(name = "count_species")
#> # A tibble: 1 x 1
#> count_species
#> <int>
#> 1 3
# basically same as
iris %>%
distinct(Species) %>%
tally()
#> n
#> 1 3
# can pull all of above, but no pull here!
# Thanks Michael Sumner
iris %>%
group_by(Species) %>%
n_groups()
#> [1] 3
# Thanks Nick Tierney
iris %>%
pull(Species) %>%
n_distinct()
#> [1] 3
# also works, need to pull
iris %>%
summarize(n_species = n_distinct(Species)) %>%
pull()
#> [1] 3
```
<sup>Created on 2019-07-16 by the [reprex package](https://reprex.tidyverse.org) (v0.2.1)</sup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment