Last active
July 24, 2018 15:04
-
-
Save grayskripko/a047b2a7df894b49b423623a490bd39d to your computer and use it in GitHub Desktop.
tally() and count()
This file contains hidden or 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(dplyr) | |
| x <- tibble(a=c(1, 2, 2, 3)) | |
| x %>% group_by(a) %>% summarise(n()) | |
| #> # A tibble: 3 x 2 | |
| #> a `n()` | |
| #> <dbl> <int> | |
| #> 1 1 1 | |
| #> 2 2 2 | |
| #> 3 3 1 | |
| x %>% group_by(a) %>% tally() | |
| #> # A tibble: 3 x 2 | |
| #> a n | |
| #> <dbl> <int> | |
| #> 1 1 1 | |
| #> 2 2 2 | |
| #> 3 3 1 | |
| x %>% count(a) | |
| #> # A tibble: 3 x 2 | |
| #> a n | |
| #> <dbl> <int> | |
| #> 1 1 1 | |
| #> 2 2 2 | |
| #> 3 3 1 | |
| x %>% group_by(a) %>% add_tally() | |
| #> # A tibble: 4 x 2 | |
| #> # Groups: a [3] | |
| #> a n | |
| #> <dbl> <int> | |
| #> 1 1 1 | |
| #> 2 2 2 | |
| #> 3 2 2 | |
| #> 4 3 1 | |
| x %>% add_count(a) | |
| #> # A tibble: 4 x 2 | |
| #> a n | |
| #> <dbl> <int> | |
| #> 1 1 1 | |
| #> 2 2 2 | |
| #> 3 2 2 | |
| #> 4 3 1 | |
| x %>% group_by(a) %>% tally(a) | |
| #> # A tibble: 3 x 2 | |
| #> a n | |
| #> <dbl> <dbl> | |
| #> 1 1 1 | |
| #> 2 2 4 | |
| #> 3 3 3 | |
| x %>% group_by(a) %>% add_tally(a) | |
| #> # A tibble: 4 x 2 | |
| #> # Groups: a [3] | |
| #> a n | |
| #> <dbl> <dbl> | |
| #> 1 1 1 | |
| #> 2 2 4 | |
| #> 3 2 4 | |
| #> 4 3 3 | |
| x %>% count() | |
| #> # A tibble: 1 x 1 | |
| #> n | |
| #> <int> | |
| #> 1 4 | |
| x %>% add_count() | |
| #> # A tibble: 4 x 2 | |
| #> a n | |
| #> <dbl> <int> | |
| #> 1 1 4 | |
| #> 2 2 4 | |
| #> 3 2 4 | |
| #> 4 3 4 | |
| ``` | |
| Created on 2018-07-24 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment