long と wide

long データ (縦長のデータ)

tidyr::table1 |> 
  dplyr::select(!population)

longデータからwideデータへの変換

  • pivot_wider() : wide型 (← 長 ) のtibbleに変換
tidyr::table1 |> 
  dplyr::select(!population) |> 
  tidyr::pivot_wider(
    names_from = year,  # ラベルにしたい列名
    values_from = cases # 値にしたい列名
  )

wide データ (横長のデータ)

tidyr::table4a

wideデータからlongデータへの変換

  • pivot_longer() : long型 (← 長 ) のtibbleに変換
tidyr::table4a |> 
  tidyr::pivot_longer(
    cols = `1999`:`2000`, # 展開したい列を全て選択. ベクトルでも可
    names_to = "year",
    values_to = "cases"
  )