令和4年 悩みやストレスの原因
Rで令和4年の国民生活基礎調査から年齢別の悩みやストレスの原因を面グラフを作るまで。 e-statのAPIを使って対象のデータを取得して、先頭の150行を表示。 options(crayon.enabled = FALSE) # 色無効化 library(tidyverse) library(httr) library(Polychrome) # palette36 __ ── Attaching core tidyverse packages ──────────────────────────────────────────────────────────── tidyverse 2.0.0 ── ✔ dplyr 1.1.4 ✔ readr 2.1.5 ✔ forcats 1.0.0 ✔ stringr 1.5.1 ✔ ggplot2 3.5.2 ✔ tibble 3.2.1 ✔ lubridate 1.9.4 ✔ tidyr 1.3.1 ✔ purrr 1.0.4 ── Conflicts ────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ── ✖ dplyr::filter() masks stats::filter() ✖ dplyr::lag() masks stats::lag() ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors apikey = "1234abcdxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # e-statで取得した自分用のAPIキー # 国民生活基礎調査 令和4年国民生活基礎調査 健康 # 世帯人員(12歳以上),悩みやストレスの有-悩みやストレスの原因(複数回答)-無・性・年齢(5歳階級)別 # https://www.e-stat.go.jp/dbview?sid=0002040955 c <- paste0("http://api.e-stat.go.jp/rest/3.0/app/getSimpleStatsData?appId=", apikey, "&lang=J&statsDataId=0002040955&metaGetFlg=Y&cntGetFlg=N&explanationGetFlg=Y&annotationGetFlg=Y§ionHeaderFlg=1&replaceSpChars=0") res <- GET(c) text <- content(res, as = "text", encoding = "UTF-8") lines <- strsplit(text, "\n")[[1]] head(lines, 150) ...