LoginSignup
15
18

More than 5 years have passed since last update.

purrrでdoせず楽をする

Last updated at Posted at 2015-11-12

@uri のこの記事、do()の使い方がまとまっていてとてもいいです。

do()の使い方がよくわからなくて、doしますか!? ダーッ!しますか!?みたいに頭が混乱していても、元気があれば何でもできそうな気さえしてきます。

dplyrでdoして楽をする - Qiita

ということで特に文句をつけるところはないですが、最後の例をpurrrで書くとこう、というのをいちおう書き残しておきます。

library(purrr)

iris %>%
  split(.$Species) %>%
  map2(., names(.), ~ ggplot(.x, aes(Sepal.Length, Petal.Length)) + geom_point() + ggtitle(.y))

今のところ、値とキーの両方にアクセスする便利な方法は用意されていないので、map2(., names(.), ~ f(.x, .y))のような書き方をすることになります。将来的にはもうちょっとわかりやすい書き方ができるようになってほしいところ。


追記:

今はimap()という関数があるのでこれを使いましょう。

library(purrr)

iris %>%
  split(.$Species) %>%
  imap(~ ggplot(.x, aes(Sepal.Length, Petal.Length)) + geom_point() + ggtitle(.y))
15
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
18