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
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
## Use a numeric value for sep
flights %>%
separate(dep_time, into = c("dep_hour", "dep_minute"), sep = -3, convert = TRUE) %>%
separate(arr_time, into = c("arr_hour", "arr_minute"), sep = -3, convert = TRUE) %>%
select(starts_with("dep_"), starts_with("arr_"))
## # A tibble: 336,776 x 6
## dep_hour dep_minute dep_delay arr_hour arr_minute arr_delay
## * <int> <int> <dbl> <int> <int> <dbl>
## 1 5 17 2.00 8 30 11.0
## 2 5 33 4.00 8 50 20.0
## 3 5 42 2.00 9 23 33.0
## 4 5 44 -1.00 10 4 -18.0
## 5 5 54 -6.00 8 12 -25.0
## 6 5 54 -4.00 7 40 12.0
## 7 5 55 -5.00 9 13 19.0
## 8 5 57 -3.00 7 9 -14.0
## 9 5 57 -3.00 8 38 - 8.00
## 10 5 58 -2.00 7 53 8.00
## # ... with 336,766 more rows
Copyright © 2017 Kirill Müller. Licensed under CC BY-NC 4.0.