Elements of Data Science
SDS 322E

H. Sherry Zhang
Department of Statistics and Data Sciences
University of Texas at Austin

Get the repository for today:
library(usethis)
create_from_github("SDS322E-26FALL/0503-pivot2", fork = FALSE)

Learning objectives

  • More complicated pivot scenarios

More complicated pivot

us_rent_income
# A tibble: 104 × 5
  GEOID NAME    variable estimate   moe
  <chr> <chr>   <chr>       <dbl> <dbl>
1 01    Alabama income      24476   136
2 01    Alabama rent          747     3
3 02    Alaska  income      32940   508
4 02    Alaska  rent         1200    13
5 04    Arizona income      27517   148
# ℹ 99 more rows

Target:

# A tibble: 52 × 6
  GEOID NAME       estimate_income estimate_rent moe_income moe_rent
  <chr> <chr>                <dbl>         <dbl>      <dbl>    <dbl>
1 01    Alabama              24476           747        136        3
2 02    Alaska               32940          1200        508       13
3 04    Arizona              27517           972        148        4
4 05    Arkansas             23789           709        165        5
5 06    California           29454          1358        109        3
# ℹ 47 more rows

Syntax:

pivot_wider(names_from = ..., values_from = ...)

More complicated pivot: multiple variables

us_rent_income |>
  pivot_wider(names_from = "variable", values_from = c("estimate", "moe"))
# A tibble: 52 × 6
  GEOID NAME       estimate_income estimate_rent moe_income moe_rent
  <chr> <chr>                <dbl>         <dbl>      <dbl>    <dbl>
1 01    Alabama              24476           747        136        3
2 02    Alaska               32940          1200        508       13
3 04    Arizona              27517           972        148        4
4 05    Arkansas             23789           709        165        5
5 06    California           29454          1358        109        3
# ℹ 47 more rows

useful argument names_vary: “fastest” varies names_from values fastest, resulting in a column naming scheme of the form: ⁠value1_name1, value1_name2, value2_name1, value2_name2⁠. This is the default.

us_rent_income |>
  pivot_wider(names_from = "variable", values_from = c("estimate", "moe"), names_vary = "fastest")
# A tibble: 52 × 6
  GEOID NAME       estimate_income estimate_rent moe_income moe_rent
  <chr> <chr>                <dbl>         <dbl>      <dbl>    <dbl>
1 01    Alabama              24476           747        136        3
2 02    Alaska               32940          1200        508       13
3 04    Arizona              27517           972        148        4
4 05    Arkansas             23789           709        165        5
5 06    California           29454          1358        109        3
# ℹ 47 more rows

More complicated pivot: multiple variables

useful argument names_vary: “slowest” varies names_from values slowest, resulting in a column naming scheme of the form: ⁠value1_name1, value2_name1, value1_name2, value2_name2⁠.

us_rent_income |>
  pivot_wider(names_from = "variable", values_from = c("estimate", "moe"),
              names_vary = "slowest")
# A tibble: 52 × 6
  GEOID NAME       estimate_income moe_income estimate_rent moe_rent
  <chr> <chr>                <dbl>      <dbl>         <dbl>    <dbl>
1 01    Alabama              24476        136           747        3
2 02    Alaska               32940        508          1200       13
3 04    Arizona              27517        148           972        4
4 05    Arkansas             23789        165           709        5
5 06    California           29454        109          1358        3
# ℹ 47 more rows

Your time (1/2)

usethis::create_from_github("SDS322E-26FALL/0503-pivot2", fork = FALSE)

Explain why the code below results an error - How would you fix it?

billboard |>
  pivot_longer(-artist, names_to = "week", values_to = "rank")

Error in pivot_longer(): ! Can’t combine track and date.entered . Run rlang::last_trace() to see where the error occurred.

Your time (2/2)

Tables below show the number of TB cases documented by WHO in a different layout. Can you reshape them to table1?

table1
# A tibble: 6 × 4
  country      year  cases population
  <chr>       <dbl>  <dbl>      <dbl>
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
# ℹ 1 more row
table2
# A tibble: 12 × 4
  country      year type          count
  <chr>       <dbl> <chr>         <dbl>
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
# ℹ 7 more rows
table3
# A tibble: 6 × 3
  country      year rate             
  <chr>       <dbl> <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
# ℹ 1 more row
table4a 
# A tibble: 3 × 3
  country     `1999` `2000`
  <chr>        <dbl>  <dbl>
1 Afghanistan    745   2666
2 Brazil       37737  80488
3 China       212258 213766
table4b
# A tibble: 3 × 3
  country         `1999`     `2000`
  <chr>            <dbl>      <dbl>
1 Afghanistan   19987071   20595360
2 Brazil       172006362  174504898
3 China       1272915272 1280428583