── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# A tibble: 650 × 1
constituency
<chr>
1 Aberavon
2 Aberconwy
3 Aberdeen North
4 Aberdeen South
5 Aberdeenshire West & Kincardine
6 Airdrie & Shotts
7 Aldershot
8 Aldridge-Brownhills
9 Altrincham & Sale West
10 Alyn & Deeside
# ℹ 640 more rows
Tally them up:
ukvote2019 |>distinct(constituency) |>tally()
# A tibble: 1 × 1
n
<int>
1 650
# Base R / non-pipeline versionlength(unique(ukvote2019$constituency))
[1] 650
Which parties fielded the most candidates?
ukvote2019 |>count(party_name) |>arrange(desc(n))
# A tibble: 69 × 2
party_name n
<chr> <int>
1 Conservative 636
2 Labour 631
3 Liberal Democrat 611
4 Green 497
5 The Brexit Party 275
6 Independent 224
7 Scottish National Party 59
8 UKIP 44
9 Plaid Cymru 36
10 Christian Peoples Alliance 29
# ℹ 59 more rows
Top 5:
ukvote2019 |>count(party_name) |>slice_max(order_by = n, n =5)
# A tibble: 5 × 2
party_name n
<chr> <int>
1 Conservative 636
2 Labour 631
3 Liberal Democrat 611
4 Green 497
5 The Brexit Party 275
Bottom 5:
ukvote2019 |>count(party_name) |>slice_min(order_by = n, n =5)
# A tibble: 25 × 2
party_name n
<chr> <int>
1 Ashfield Independents 1
2 Best for Luton 1
3 Birkenhead Social Justice Party 1
4 British National Party 1
5 Burnley & Padiham Independent Party 1
6 Church of the Militant Elvis Party 1
7 Citizens Movement Party UK 1
8 CumbriaFirst 1
9 Heavy Woollen District Independents 1
10 Independent Network 1
# ℹ 15 more rows
How many constituencies are there again?
ukvote2019 |>count(constituency)
# A tibble: 650 × 2
constituency n
<chr> <int>
1 Aberavon 7
2 Aberconwy 4
3 Aberdeen North 6
4 Aberdeen South 4
5 Aberdeenshire West & Kincardine 4
6 Airdrie & Shotts 5
7 Aldershot 4
8 Aldridge-Brownhills 5
9 Altrincham & Sale West 6
10 Alyn & Deeside 5
# ℹ 640 more rows
ukvote2019 |>distinct(constituency) |>count()
# A tibble: 1 × 1
n
<int>
1 650
# Base R style ...length(unique(ukvote2019$constituency))
[1] 650
Counting Twice Over
What does this mean?
ukvote2019 |>count(constituency) |>count(n)
Storing counts in `nn`, as `n` already present in input
ℹ Use `name = "new_name"` to pick a new name.
---title: "Example 05: more dplyr"---## Setup```{r}library(here) # manage file pathslibrary(socviz) # data and some useful functionslibrary(tidyverse) # your friend and mine```## UK Election Data```{r}#| label: "03b-dplyr-basics-16"# remotes::install_github("kjhealy/ukelection2019")library(ukelection2019)ukvote2019```Use [**`sample_n()`**]{.fg-green} to sample `n` rows of your tibble.```{r }#| label: "03b-dplyr-basics-17"library(ukelection2019)ukvote2019 |>sample_n(10)```A vector of unique constituency names:```{r}#| label: "03b-dplyr-basics-18"ukvote2019 |>distinct(constituency)```Tally them up:```{r}#| label: "03b-dplyr-basics-19"ukvote2019 |>distinct(constituency) |>tally()``````{r}#| label: "03b-dplyr-basics-20"# Base R / non-pipeline versionlength(unique(ukvote2019$constituency))```Which parties fielded the most candidates?```{r}#| label: "03b-dplyr-basics-21"ukvote2019 |>count(party_name) |>arrange(desc(n))```Top 5:```{r}#| label: "03b-dplyr-basics-22"ukvote2019 |>count(party_name) |>slice_max(order_by = n, n =5)```Bottom 5:```{r}#| label: "03b-dplyr-basics-23"ukvote2019 |>count(party_name) |>slice_min(order_by = n, n =5)```How many constituencies are there again?```{r}#| label: "03b-dplyr-basics-24"ukvote2019 |>count(constituency) ``````{r}ukvote2019 |>distinct(constituency) |>count()``````{r}# Base R style ...length(unique(ukvote2019$constituency))```## Counting Twice OverWhat does this mean?```{r}#| label: "03b-dplyr-basics-25"ukvote2019 |>count(constituency) |>count(n)```