# Setupチャンクに書く
row_data_d_2023 <- read_csv("data/SSDSE-D-2023.csv",
locale = locale(encoding = "CP932"))
row_data_d_2021 <- read_csv("data/SSDSE-D-2021.csv",
locale = locale(encoding = "CP932"))
df_long_d_2023 <-
row_data_d_2023 |>
select("SSDSE-D-2023", Prefecture, matches("^[A-Z]{2}[0-9]{2}$")) |> # 「MA01」「MB12」などのように、アルファベット2文字+数字2桁 になっている列をまとめて選ぶ
select(-c(MA00, MB00, MC00, MD00, ME00, MF00)) |> # 複数の列を一括で削除するためにc()で囲む
pivot_longer(
# 横に並んでいるデータ(列)を、縦に並べ替える
cols = matches("^[A-Z]{2}[0-9]{2}$"), # 対象となる列(コード列)
names_to = "code", # 列名(MA01など)を「code」という列に入れる
values_to = "value"
) # 中の値を「value」という列に入れる
middle_category <-
df_long_d_2023 |>
filter(Prefecture == "都道府県") |>
select(code, category_middle = value)
df_long_cat_d_2023 <-
df_long_d_2023 |>
mutate(Prefecture = factor(Prefecture, levels = pref_levels)) |>
filter_out(Prefecture == "都道府県") |>
filter_out(`SSDSE-D-2023` == "男女の別") |>
drop_na(Prefecture) |>
left_join(middle_category, by = "code") |>
relocate(category_middle, .after = code) |>
mutate(
category_major = case_when(
substr(code, 1, 2) == "MB" ~ "学習・自己啓発・訓練",
substr(code, 1, 2) == "MC" ~ "スポーツ",
substr(code, 1, 2) == "MD" ~ "趣味・娯楽",
substr(code, 1, 2) == "ME" ~ "ボランティア",
substr(code, 1, 2) == "MF" ~ "旅行・行楽",
substr(code, 1, 2) == "MG" ~ "行動種別",
substr(code, 1, 2) == "MH" ~ "平均時刻",
TRUE ~ NA_character_
)
) |>
relocate(category_major, .after = code) |>
rename(都道府県 = Prefecture) |>
filter_out(`SSDSE-D-2023` == "0_総数") |>
separate(`SSDSE-D-2023`, into = c(NA, "性別"), sep = "_") |>
filter(性別 %in% c("男", "女")) |>
mutate(性別 = factor(性別, levels = c("男", "女"))) |>
filter_out(都道府県 == "全国") |>
drop_na(都道府県) |>
mutate(year = 2021, .before = 性別) |>
mutate(data_file_name = "SSDSE-D-2023", .before = year) # 同種の複数データを結合する際に識別できるようファイル名を追加
df_long_d_2021 <-
row_data_d_2021 |>
select("SSDSE-D-2021", Prefecture, matches("^[A-Z]{2}[0-9]{2}$")) |> # 「MA01」「MB12」などのように、アルファベット2文字+数字2桁 になっている列をまとめて選ぶ
select(-c(MA00, MB00, MC00, MD00, ME00, MF00)) |> # 複数の列を一括で削除するためにc()で囲む
pivot_longer(
# 横に並んでいるデータ(列)を、縦に並べ替える
cols = matches("^[A-Z]{2}[0-9]{2}$"), # 対象となる列(コード列)
names_to = "code", # 列名(MA01など)を「code」という列に入れる
values_to = "value"
) # 中の値を「value」という列に入れる
middle_category_2021 <-
df_long_d_2021 |>
filter(Prefecture == "都道府県") |>
select(code, category_middle = value)
df_long_cat_d_2021 <-
df_long_d_2021 |>
mutate(Prefecture = factor(Prefecture, levels = pref_levels)) |>
filter_out(Prefecture == "都道府県") |>
filter_out(`SSDSE-D-2021` == "男女の別") |>
drop_na(Prefecture) |>
left_join(middle_category, by = "code") |>
relocate(category_middle, .after = code) |>
mutate(
category_major = case_when(
substr(code, 1, 2) == "MB" ~ "学習・自己啓発・訓練",
substr(code, 1, 2) == "MC" ~ "スポーツ",
substr(code, 1, 2) == "MD" ~ "趣味・娯楽",
substr(code, 1, 2) == "ME" ~ "ボランティア",
substr(code, 1, 2) == "MF" ~ "旅行・行楽",
substr(code, 1, 2) == "MG" ~ "行動種別",
substr(code, 1, 2) == "MH" ~ "平均時刻",
TRUE ~ NA_character_
)
) |>
relocate(category_major, .after = code) |>
rename(都道府県 = Prefecture) |>
filter_out(`SSDSE-D-2021` == "0_総数") |>
separate(`SSDSE-D-2021`, into = c(NA, "性別"), sep = "_") |>
filter(性別 %in% c("男", "女")) |>
mutate(性別 = factor(性別, levels = c("男", "女"))) |>
filter_out(都道府県 == "全国") |>
drop_na(都道府県) |>
mutate(year = 2016, .before = 性別) |>
mutate(data_file_name = "SSDSE-D-2021", .before = year) # 同種の複数データを結合する際に識別できるようファイル名を追加