table3

table3 %>%
  separate(rate, into = c("cases", "population"), sep = "/", convert = TRUE)
## # A tibble: 6 x 4
##   country      year  cases population
## * <chr>       <int>  <int>      <int>
## 1 Afghanistan  1999    745   19987071
## 2 Afghanistan  2000   2666   20595360
## 3 Brazil       1999  37737  172006362
## 4 Brazil       2000  80488  174504898
## 5 China        1999 212258 1272915272
## 6 China        2000 213766 1280428583
table3 %>%
  separate(
    rate,
    into = c("cases", "population"),
    sep = "/",
    convert = TRUE
  ) %>%
  gather(type, count, -country, -year) %>%
  arrange(country, year, type)
## # A tibble: 12 x 4
##    country      year type            count
##    <chr>       <int> <chr>           <int>
##  1 Afghanistan  1999 cases             745
##  2 Afghanistan  1999 population   19987071
##  3 Afghanistan  2000 cases            2666
##  4 Afghanistan  2000 population   20595360
##  5 Brazil       1999 cases           37737
##  6 Brazil       1999 population  172006362
##  7 Brazil       2000 cases           80488
##  8 Brazil       2000 population  174504898
##  9 China        1999 cases          212258
## 10 China        1999 population 1272915272
## 11 China        2000 cases          213766
## 12 China        2000 population 1280428583

To table3

table2 %>%
  spread(type, count) %>% 
  unite(rate, cases, population, sep = "/")
## # A tibble: 6 x 3
##   country      year rate             
##   <chr>       <int> <chr>            
## 1 Afghanistan  1999 745/19987071     
## 2 Afghanistan  2000 2666/20595360    
## 3 Brazil       1999 37737/172006362  
## 4 Brazil       2000 80488/174504898  
## 5 China        1999 212258/1272915272
## 6 China        2000 213766/1280428583

Counting relations

flights %>%
  unite(
    relation,
    origin, dest,
    sep = " -> "
  ) %>%
  count(relation)
## # A tibble: 224 x 2
##    relation       n
##    <chr>      <int>
##  1 EWR -> ALB   439
##  2 EWR -> ANC     8
##  3 EWR -> ATL  5022
##  4 EWR -> AUS   968
##  5 EWR -> AVL   265
##  6 EWR -> BDL   443
##  7 EWR -> BNA  2336
##  8 EWR -> BOS  5327
##  9 EWR -> BQN   297
## 10 EWR -> BTV   931
## # ... with 214 more rows

Copyright © 2018 Kirill Müller. Licensed under CC BY-NC 4.0.