裏 RjpWiki

Julia ときどき R, Python によるコンピュータプログラム,コンピュータ・サイエンス,統計学

plyr なんて...(その14)

2015年11月12日 | ブログラミング

今更なんだけど,ある変数が取る値ごとのサンプルサイズ

> f1 = function() count(aq, .(Month))
> (ans = f1())
  Month freq
1     5 3100
2     6 3000
3     7 3100
4     8 3100
5     9 3000
>
> f2 = function() {
+     a = ddply(aq, "Month", nrow)
+     colnames(a)[2] = "freq"
+     a
+     }
> identical(ans, f2())
[1] TRUE
>
> f3 = function() {
+     a = sapply(split(aq, aq$Month), nrow)
+     data.frame(Month=as.integer(names(a)), freq=a, row.names=NULL)
+     }
> identical(ans, f3())
[1] TRUE
>
> f4 = function() {
+     a = table(aq$Month)
+     data.frame(Month=as.integer(names(a)), freq=c(a), row.names=NULL)
+     }
> identical(ans, f4())
[1] TRUE
>
> f5 = function() {
+     b = 5:9
+     a = sapply(b, function(i) sum(aq$Month==i))
+     data.frame(Month=b, freq=a)
+     }
> identical(ans, f5())
[1] TRUE
>
> # sapply を for 代わりに使った f5 は,count に比べて 2 倍速
> benchmark(f1(), f2(), f3(), f4(), f5(), columns=c("test", "replications", "elapsed", "relative", "user.self", "sys.self"), replications=1000, order=NULL)
  test replications elapsed relative user.self sys.self
1 f1()         1000   1.853    2.230     1.686    0.183
2 f2()         1000   4.336    5.218     4.039    0.317
3 f3()         1000   3.919    4.716     3.632    0.334
4 f4()         1000   4.861    5.850     4.698    0.184
5 f5()         1000   0.831    1.000     0.769    0.066

コメント    この記事についてブログを書く
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする
« plyr なんて...(その13) | トップ | plyr 結論 »
最新の画像もっと見る

コメントを投稿

ブログラミング」カテゴリの最新記事