本日の目標

  1. group_by() + summarise() でグループ別集計ができる
  2. count() でカテゴリの頻度を集計できる
  3. 集計結果を ggplot2 でグラフ化できる

1. summarise():集計

summarise() はデータ全体を1行(または数行)に集約します。

# msleep の全体の睡眠時間の平均
msleep |>
  summarise(mean_sleep = mean(sleep_total, na.rm = TRUE))
# 複数の統計量をまとめて計算
msleep |>
  summarise(
    n = n(),
    mean_sleep = mean(sleep_total, na.rm = TRUE),
    sd_sleep   = sd(sleep_total, na.rm = TRUE),
    min_sleep  = min(sleep_total, na.rm = TRUE),
    max_sleep  = max(sleep_total, na.rm = TRUE)
  )

よく使う集計関数

関数 意味
mean(x, na.rm=TRUE) 平均値
sd(x, na.rm=TRUE) 標準偏差
median(x, na.rm=TRUE) 中央値
min(x) / max(x) 最小値 / 最大値
n() データ件数
sum(x, na.rm=TRUE) 合計

na.rm = TRUE は欠損値(NA)を除いて計算するオプションです。


2. group_by() + summarise():グループ別集計

group_by()summarise() を組み合わせると,グループごとに集計できます。

# 食性(vore)ごとの平均睡眠時間
msleep |>
  group_by(vore) |>
  summarise(
    n = n(),
    mean_sleep = mean(sleep_total, na.rm = TRUE),
    sd_sleep   = sd(sleep_total, na.rm = TRUE)
  )
# irisデータ:種ごとの平均と標準偏差
iris |>
  group_by(Species) |>
  summarise(
    n = n(),
    mean_sepal = mean(Sepal.Length),
    sd_sepal   = sd(Sepal.Length),
    mean_petal = mean(Petal.Length),
    sd_petal   = sd(Petal.Length)
  )

3. count():カテゴリの頻度集計

# 食性ごとの件数
msleep |>
  count(vore)
# 降順で並べる
msleep |>
  count(vore, sort = TRUE)

4. 集計結果をグラフ化する

棒グラフ(平均値の比較)

# 集計データを作成
sleep_summary <- msleep |>
  filter(!is.na(vore)) |>
  group_by(vore) |>
  summarise(
    mean_sleep = mean(sleep_total, na.rm = TRUE),
    sd_sleep   = sd(sleep_total, na.rm = TRUE),
    n          = n()
  )

# 棒グラフ(geom_col で集計済みデータを使う)
ggplot(sleep_summary, aes(x = vore, y = mean_sleep, fill = vore)) +
  geom_col() +
  labs(title = "食性ごとの平均睡眠時間",
       x = "食性", y = "平均睡眠時間(時間)",
       fill = "食性") +
  theme_bw()

エラーバー付き棒グラフ

ggplot(sleep_summary, aes(x = vore, y = mean_sleep, fill = vore)) +
  geom_col() +
  geom_errorbar(aes(ymin = mean_sleep - sd_sleep,
                    ymax = mean_sleep + sd_sleep),
                width = 0.2) +
  labs(title = "食性ごとの平均睡眠時間(±SD)",
       x = "食性", y = "平均睡眠時間(時間)") +
  theme_bw() +
  theme(legend.position = "none")


5. 欠損値の扱い

# 欠損値がある列の確認
msleep |>
  summarise(across(everything(), ~ sum(is.na(.))))
# 欠損値を除いて集計(na.rm = TRUE)
msleep |>
  summarise(mean_rem = mean(sleep_rem, na.rm = TRUE))
# 欠損値のある行を除いてから集計
msleep |>
  filter(!is.na(sleep_rem)) |>
  group_by(vore) |>
  summarise(
    n = n(),
    mean_rem = mean(sleep_rem)
  )

6. 実際のCSVファイルの読み込み

# CSVファイルの読み込み(readrパッケージ,tidyverseに含まれる)
my_data <- read_csv("data/my_data.csv")

# 文字コードを指定する場合(日本語データ)
my_data <- read_csv("data/my_data.csv", locale = locale(encoding = "UTF-8"))

# Excelファイルを読む場合(readxlパッケージが必要)
# install.packages("readxl")
library(readxl)
my_data <- read_excel("data/my_data.xlsx")

演習

演習1:グループ別集計

iris データを使って,種(Species)ごとに以下を計算してください。

  • Petal.Length の平均・標準偏差・サンプルサイズ
iris |>
  group_by(___) |>
  summarise(
    n    = n(),
    mean = mean(___),
    sd   = sd(___)
  )

演習2:集計結果のグラフ化

演習1で作った集計結果を使って,エラーバー付きの棒グラフを作成してください。

# まず集計
petal_summary <- iris |>
  group_by(Species) |>
  summarise(
    mean_petal = mean(Petal.Length),
    sd_petal   = sd(Petal.Length)
  )

# グラフ
ggplot(petal_summary, aes(x = ___, y = ___, fill = ___)) +
  geom_col() +
  geom_errorbar(aes(ymin = ___ - ___, ymax = ___ + ___), width = 0.2) +
  labs(title = "種ごとの花びらの長さ(平均±SD)",
       x = "___", y = "___") +
  theme_bw()

演習3:count()の応用

msleep で欠損値がない場合の食性(vore)ごとのサンプルサイズを count() を使って集計し,多い順に並べてください。


本日のまとめ

  • summarise() でデータを集計できる
  • group_by() と組み合わせるとグループ別集計が可能
  • count() でカテゴリの頻度を数えられる
  • 集計結果を ggplot2 でグラフ化できる
  • na.rm = TRUE で欠損値を除いた計算ができる

宿題・予習

  • 本日の演習を仕上げ,スクリプトを保存しておく
  • 次回は習熟度確認テスト(第1回)と復習です
  • 第2〜6回で学んだ内容(可視化・加工)を見直しておく