Functional Programming Tools • purrr

Function reference • purrr

dplyr 0.8からgroup_mapが追加されたことにより、nest()->map()->unnese()の流れからそちらに移行することになるか?

base::split() vs. group_by() and nest()

Pros and cons of split() vs nest() with map() workflows - tidyverse - RStudio Community

[ ] splitとgroup_by and nestの練習 @Yuho T map_lgl(), map_dbl(), map_int(), map_chr()

map()がリストを返すのに対し、型の決まったベクトル(論理、実数、整数、文字)を返す。base::sapply(), base::vapply()の大体ができる。特にsapply()は長さがゼロだとリストを返したりするので、確実に決まった型のベクトルで操作することができる利点がある。

numericな列を選択してShapiro検定で正規性を検討、p<0.05の列をリスト化 主にtableoneパッケージでの変数リスト作成用に。

# sapplyで添字利用
iris %>% .[sapply(., is.numeric)] %>%
  purrr::map(~ broom::tidy(shapiro.test(.x))) %>% 
  purrr::map_lgl(~ .$p.value < 0.05) %>%
  .[. == TRUE] %>%
  names %>%
  dput


# map_lgl()利用
iris %>% 
  .[purrr::map_lgl(., is.numeric)] %>% # 以下略

使えるときは一番単純な手段で行うべし、的にはこのdplyr::select_if()がベストか。

# dplyr::select_if()利用
iris %>% 
  dplyr::select_if(is.numeric) %>% # 以下略

keep(), discard()

Keep or discard elements using a predicate function. — keep • purrr

imap()

purrrでdoせず楽をする - Qiita

rerun

サンプルデータ作るのに死ぬほど便利そう。 rerun function | R Documentation