2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

R:plyrパッケージのWindows環境化での並列化

Posted at

そういえば長いこと投稿していなかった。。。

Rでデータの分割・処理適用・結合を行う上でとても便利なパッケージのひとつにplyrさんがあります。

以下のスライドをみてから使い始めましたが、今では欠かせないパッケージのひとつとなっています。
http://www.slideshare.net/teramonagi/tokyo-r30-20130420

このplyrさんのXXply系の関数には「.parallel」という並列処理を可能とするオプションがあります。

マルチスレッドなCPUと潤沢なメモリがあるなら、それを有効活用して大量データも効率よく処理しましょう。

Windows環境化では、doParallelパッケージを使うことでこのオプションを有効にすることができます(64bit Windows7環境にて確認)。

# ライブラリを読み込む
library(plyr)
library(doParallel)

# 並列数の設定
registerDoParallel(makeCluster(7))

# 処理対象データの読み込み
d <- read.csv('data.csv', stringsAsFactors=F)

# ddplyで入出力共にデータフレームとなる独自の処理を実行
result <- ddply(d, "group", .parallel=TRUE, function(targets){...})

タスクマネージャーをみると、並列処理を有効にした場合はCPUがガリガリ仕事をしていることがわかります。

ちなみに進捗状況を示す「.progress="text"」のオプションは、並列処理時には使えません。こんな警告が出ます。

Progress disabled when using parallel plyr

.parallelオプションのおかげでplyrさんの利便性を保ちつつ、ネックである処理の遅さをある程度解決できます。

ばんざーい。

以上

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?