- 追加された行はこのように表示されます。
- 削除された行は
このように表示されます。
!map関数を使ってグループ単位に処理を適用する
map関数についてはTokyoRの@kilometerさんの記事がよくわかる。
https://qiita.com/kilometer/items/7184904765fbf0f33f04
ここではハマった内容を残しておく。
* 間違い(動かない)
iris |> group_by(Species) |> nest() |> mutate(result = map(.x = data, .f=~shapiro.test(Sepal.Length)))
Error in `mutate()`:
ℹ In argument: `result = map(.x = data, .f = ~shapiro.test(Sepal.Length))`.
ℹ In group 1: `Species = setosa`.
Caused by error in `map()`:
ℹ In index: 1.
Caused by error in `stopifnot()`:
! オブジェクト 'Sepal.Length' がありません
* 正しい(r-wakalangでKDさんに教わった)
iris |> group_by(Species) |> nest() |> mutate(result = map(.x = data, .f=~shapiro.test(.x$Sepal.Length)))
map()の中で指定する関数に与える変数の指定方法に注意。
!同じ目的を達成するには別の方法がある(by KD)
* 基本
iris |>
summarize(result = list(shapiro.test(Sepal.Length)), .by = Species) |>
unpack(result)
* 解析結果をきれいな data.frame として持ちたいとき
iris |>
summarize(result = broom::tidy(shapiro.test(Sepal.Length)), .by = Species) |>
unpack(result)
* 解析結果を list として持ちたいとき
iris |>
summarize(result = list(shapiro.test(Sepal.Length)), .by = Species) |>
deframe()