tidydata
Pivoting
Separating and uniting
Separate a column
table3_sep <- table3 %>%
separate(col = rate, into = c("cases", "population"))
Unite two columns
table3_sep %>%
unite(col = "rate", c(cases,population), sep = "/")
## # A tibble: 6 × 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
Missing Values
stocks <- tibble(
year = c(2015, 2015, 2015, 2015, 2016, 2016, 2016),
qtr = c( 1, 2, 3, 4, 2, 3, 4),
return = c(1.88, 0.59, 0.35, NA, 0.92, 0.17, 2.66)
)
stocks %>%
pivot_wider(names_from = year, values_from = return)
## # A tibble: 4 × 3
## qtr `2015` `2016`
## <dbl> <dbl> <dbl>
## 1 1 1.88 NA
## 2 2 0.59 0.92
## 3 3 0.35 0.17
## 4 4 NA 2.66
bikes <- tibble(
bike_model = c("A","A","B","B","C"),
material = c("steel", "aliminium", "aliminium", "steel", "steel"),
return = c(100, 200, 300, 400, 500))
bikes
## # A tibble: 5 × 3
## bike_model material return
## <chr> <chr> <dbl>
## 1 A steel 100
## 2 A aliminium 200
## 3 B aliminium 300
## 4 B steel 400
## 5 C steel 500
Non-tidy data