dplyr tricks

code

A few lesser known dplyr functions that are super useful

The following is a list of functions that are super useful in the tidyverse that help solve a wide range of problems:

# Get data from tidytues

library(tidyverse)
cocktails <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-05-26/cocktails.csv')

add_count

adds a count based on the variable supplied. Equivalent to group_by, summarize, ungroup.

cocktails %>% 
  add_count(drink) %>% 
  select(row_id, drink, n) %>% 
  filter(n >= 8) %>% 
  distinct() %>% 
  knitr::kable()
row_id drink n
1 1-900-FUK-MEUP 8
3 151 Florida Bushwacker 8
8 3-Mile Long Island Iced Tea 8
74 Amaretto Liqueur 11
88 Angelica Liqueur 12
91 Apple Cider Punch #1 8
106 Arizona Twister 9
110 Artillery Punch 8
190 Cherry Electric Lemonade 8
197 Chocolate Monkey 8
231 Egg Nog #4 11
366 Masala Chai 8
425 Radioactive Long Island Iced Tea 8
489 Sweet Sangria 8

crossing

generates all possible combinations of variables (like expand.grid, but returns a dataframe)

crossing(
  a = 1:2,
  b = c("a", "b"),
  c = c("x", "y")) %>% 
  knitr::kable()
a b c
1 a x
1 a y
1 b x
1 b y
2 a x
2 a y
2 b x
2 b y

across

character_visualization <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-06-30/character_visualization.csv')

(top_speech <- character_visualization %>% 
  filter(!(character %in% c("Editor narration", "Omnipresent narration"))) %>% 
  group_by(character) %>% 
  summarize(across(speech:depicted, sum)) %>% 
  slice_max(order_by = speech, n = 15) %>% 
  separate(character, into = c("hero", "real_name"), sep = " = ") %>% 
  mutate(pct_speech = speech/sum(speech)) %>% 
  arrange(desc(pct_speech)) %>% 
  select(-real_name) %>% 
  select(hero, pct_speech, everything()) %>% 
  mutate(pct_speech = round(pct_speech, 3)))
# A tibble: 15 x 6
   hero                   pct_speech speech thought narrative depicted
   <chr>                       <dbl>  <dbl>   <dbl>     <dbl>    <dbl>
 1 Wolverine                   0.157   3640     532       352     3537
 2 Storm                       0.152   3517    1900        99     4169
 3 Cyclops                     0.089   2052     729       193     2223
 4 Colossus                    0.078   1800     461         6     2712
 5 Rogue                       0.078   1800     611        49     1603
 6 Ariel/Sprite/Shadowcat      0.073   1690     737        66     1714
 7 Nightcrawler                0.064   1492     441         6     1839
 8 Psylocke                    0.05    1163     159       109      993
 9 Professor X                 0.049   1128     269       175     1095
10 Marvel Girl/Phoenix         0.041    957     195         2     1012
11 Havok                       0.039    902     238       172      779
12 Dazzler                     0.038    875     565         5      896
13 Forge                       0.035    818     131       118      588
14 Magneto                     0.028    651      74       176      552
15 Phoenix(2)                  0.028    651     469        40      680

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/colemanrob/robcoleman.ca, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".