Open library

library(tidyr)

Tibbles to use

table1
## # 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
table4a
## # A tibble: 3 x 3
##   country     `1999` `2000`
## * <chr>        <int>  <int>
## 1 Afghanistan    745   2666
## 2 Brazil       37737  80488
## 3 China       212258 213766
table2
## # 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

Reshape: From wide to long

table4a %>% 
  pivot_longer(
    cols = -1,
    names_to = "year",
    values_to = "cases"
  )
## # A tibble: 6 x 3
##   country     year   cases
##   <chr>       <chr>  <int>
## 1 Afghanistan 1999     745
## 2 Afghanistan 2000    2666
## 3 Brazil      1999   37737
## 4 Brazil      2000   80488
## 5 China       1999  212258
## 6 China       2000  213766

Reshape: From long to wide

table2 %>% 
  pivot_wider(
    names_from = type,
    values_from = count)
## # 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

Formative assessment

Exercise

survey %>% 
  pivot_____(names_from = "____",
              values_from = "____")

We need to transform Table 1, to look as Table 2. Fill in the blanks, correct if necessary:

Table 1
student food rate
1 fruit 5
1 vegetable 1
1 icecream 7
2 fruit 5
2 vegetable 4
2 icecream 3
3 fruit 1
3 vegetable 6
3 icecream 9

Table 2

student fruit vegetable icecream
1 5 1 7
2 5 4 3
3 1 6 9

To get the dataset run the following code

library(dplyr)

set.seed(2020)

survey <-
  tibble(
    student = rep(c(1:3),3),
    food = c(rep("fruit",3),rep("vegetable",3), rep("icecream",3)),
    rate = abs(round(rnorm(9, mean = 4, sd = 3), 0))) %>% 
  arrange(student)

survey

Solution

survey %>%
  pivot_wider(names_from = food,
              values_from = rate)
## # A tibble: 3 x 4
##   student fruit vegetable icecream
##     <int> <dbl>     <dbl>    <dbl>
## 1       1     5         1        7
## 2       2     5         4        3
## 3       3     1         6        9