{"id":630,"date":"2015-02-03T16:48:23","date_gmt":"2015-02-03T21:48:23","guid":{"rendered":"http:\/\/zhanxw.com\/blog\/?p=630"},"modified":"2015-02-03T16:48:23","modified_gmt":"2015-02-03T21:48:23","slug":"r%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/zhanxw.com\/blog\/2015\/02\/r%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b\/","title":{"rendered":"R\u51fd\u6570\u5f0f\u7f16\u7a0b"},"content":{"rendered":"<p>R\u51fd\u6570\u5f0f\u7f16\u7a0b<br \/>\nFunctional Programming in R<\/p>\n<p>R\u8bed\u8a00\u7684\u5faa\u73af\u5f88\u6162\uff0c\u540c\u65f6\u8bed\u6cd5\u6709\u5f88\u590d\u6742\uff0c\u9677\u9631\u5f88\u591a\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u9010\u884c\u904d\u5386\u4e00\u4e2a\u77e9\u9635\u6709\u4e24\u79cd\u5199\u6cd5\uff1a <\/p>\n<p>\uff081\uff09<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nfor (i in seq_len(nrow(m))){print(i)}\r\n<\/pre>\n<p>\uff082\uff09<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nfor (i in 1:nrow(m)){print(i)}\r\n<\/pre>\n<p>\u8fd9\u4e24\u4e2a\u5199\u6cd5\u90a3\u4e2a\u5bf9\uff1f\u6216\u8005\u4e24\u4e2a\u90fd\u5bf9\uff1f<br \/>\n\u719f\u6089R\u7684\u7528\u6237\u81ea\u7136\u4f1a\u7528\uff081\uff09\uff0c\u56e0\u4e3a\u5f53m\u77e9\u9635\u7684\u884c\u6570\u4e3a\u96f6\u65f6\uff0c\u53ea\u6709\uff081\uff09\u7684\u7ed3\u679c\u662f\u6b63\u786e\u7684\uff1a<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n&gt; m &lt;- matrix(, nr = 0, nc = 0)\r\n&gt; for (i in seq_len(nrow(m))){print(i)}\r\n&gt; for (i in 1:nrow(m)){print(i)}\r\n&#x5B;1] 1\r\n&#x5B;1] 0\r\n<\/pre>\n<p>\u4e3a\u4e86\u907f\u514d\u624b\u52a8\u5199\u5faa\u73af\uff0c\u4e00\u79cd\u6298\u4e2d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u7528Filter, Reduce, Map\u7b49\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u6982\u5ff5\u3002<br \/>\n\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u6bd4\u8f83\u81ea\u7136\u7684\u5904\u7406\u5217\u8868\uff08list\uff09\u6216\u8005\u5411\u91cf\uff08vector\uff09\u7c7b\u578b\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nl &lt;- as.list(seq(9, 1, -1))\r\nd &lt;- data.frame(a = c(1, 2), b = c(3, 4))\r\nm &lt;- matrix(seq(9, 1, -1), 3, 3)\r\nv &lt;- seq(9, 1, -1)\r\n\r\nisEven &lt;- function(x) { x %% 2 == 0 }\r\nisEven(1:3)\r\n\r\nFilter(isEven, l)\r\n## Filter(isEven, d) ## this does not work\r\nFilter(isEven, m)\r\nFilter(isEven, v)\r\n\r\nPosition(isEven, l)\r\n# Position(isEven, d) ## this does not work\r\nPosition(isEven, m)\r\nPosition(isEven, v)\r\n\r\nFind(isEven, l)\r\n# Find(isEven, d) ## this does not work\r\nFind(isEven, m)\r\nFind(isEven, v)\r\n<\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5bf9\u4e8e\u5217\u8868\uff08list\uff09\u6216\u8005\u5411\u91cf\uff08vector\uff09\u7c7b\u578b\uff0cR\u5185\u5efa\u7684\u51fd\u6570\u5f0f\u51fd\u6570\uff08\u66f4\u591a\u7684\u89c1<a href=\"https:\/\/stat.ethz.ch\/R-manual\/R-devel\/library\/base\/html\/funprog.html\" title=\"FuncProg\">\uff3b1\uff3d<\/a>\uff09\u8fd8\u662f\u5f88\u65b9\u4fbf\u7684\u3002<br \/>\n\u5982\u679c\u60f3\u628a\u51fd\u6570\u5f0f\u7f16\u7a0b\u73a9\u51fa\u66f4\u591a\u82b1\u6837\uff0c\u53ef\u4ee5\u770b\u770bHadley\u7684\u65b0\u4e66Adv\uff0dR\uff08\u53c2\u89c1<a href=\"http:\/\/adv-r.had.co.nz\/Functional-programming.html\" title=\"Adv-R\">\uff3b2\uff3d<\/a>\uff09\u3002<\/p>\n<p>\u8fd9\u91cc\u8fd8\u6709\u4e2a\u95ee\u9898\uff0c\u8fd9\u79cd\u51fd\u6570\u5f0f\u7684\u5199\u6cd5\u901f\u5ea6\u5982\u4f55\uff1f\u4e0b\u9762\u7684\u4ee3\u7801\u544a\u8bc9\u6211\u4eec\u51fd\u6570\u5f0f\u7f16\u7a0b\u8282\u7701\u4e86\u5199\u7a0b\u5e8f\u7684\u65f6\u95f4\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4e5f\u8bb8\u4e5f\u80fd\u8282\u7701\u8ba1\u7b97\u65f6\u95f4\u3002<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nlibrary(microbenchmark)\r\nv &lt;- seq(10000)\r\nm1 &lt;- function(v) {\r\n  v&#x5B;v%%2 == 0]\r\n}\r\nm2 &lt;- function(v) {\r\n  res &lt;- vector()\r\n  for(i in v) {\r\n    if (i %% 2 == 0) {\r\n      res &lt;- c(res, i)\r\n    }\r\n  }\r\n  res\r\n}\r\nm3 &lt;- function(v) {\r\n  Filter(isEven, v)\r\n}\r\n\r\nmicrobenchmark(res1 &lt;- m1(v),\r\n               res2 &lt;- m2(v),\r\n               res3 &lt;- m3(v), times = 1000)\r\n<\/pre>\n<p>\u4e0b\u9762\u662f\u7ed3\u679c\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nUnit: microseconds\r\n          expr       min        lq       mean    median         uq       max neval\r\n res1 &lt;- m1(v)   341.038   348.526   529.9475   356.225   380.6965  58359.46  1000\r\n res2 &lt;- m2(v) 33687.987 38433.148 48014.3656 39791.000 41751.3955 303604.96  1000\r\n res3 &lt;- m3(v)  6178.547  6648.369  8493.9365  6999.976  7445.0725 124781.90  1000\r\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<br \/>\n\u7b2c\u4e00\u79cd\u662f\u5411\u91cf\u5316\u7684\u8d4b\u503c\uff0c\u901f\u5ea6\u6700\u5feb\u3002<br \/>\n\u7b2c\u4e8c\u79cd\u662f\u7528\u5faa\u73af\uff0c\u901f\u5ea6\u6700\u6162\uff0c<br \/>\n\u7b2c\u4e09\u79cd\u662f\u51fd\u6570\u5f0f\uff0c\u901f\u5ea6\u5c45\u4e2d\u3002<\/p>\n<p>\u4e0d\u8fc7\u636e\u6211\u7684\u7ecf\u9a8c\uff0c\u5f53v\u7684\u7ef4\u6570\u5f88\u5c0f\u65f6\uff08\u6bd4\u5982\u53ea\u6709100\u4e2a\u6570\uff09\uff0c\u51fd\u6570\u5f0f\u76f8\u5bf9\u4e8e\u5faa\u574f\u7684\u4f18\u52bf\u8d8a\u6765\u8d8a\u5c0f\uff0c\u751a\u81f3\u6bd4\u5faa\u73af\u8981\u6162\u3002<\/p>\n<p>\u9898\u5916\u8bdd\uff1a\u636e\u8bf4R\u7684\u6838\u5fc3\u662f\u7c7b\u4f3c\u4e8eScheme\uff0c\u4e5f\u662f\u4e00\u79cd\u51fd\u6570\u5f0f\u8bed\u8a00\u3002\u4f46\u662f\u5728\u901f\u5ea6\u4e0a\u5bf9\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u652f\u6301\u770b\u6765\u8fd8\u6709\u5f88\u5927\u7684\u63d0\u5347\u7a7a\u95f4\u3002<\/p>\n<p>\uff3b1\uff3d<a href=\"https:\/\/stat.ethz.ch\/R-manual\/R-devel\/library\/base\/html\/funprog.html\">https:\/\/stat.ethz.ch\/R-manual\/R-devel\/library\/base\/html\/funprog.html<\/a><br \/>\n\uff3b2\uff3dAdvanced R <a href=\"http:\/\/adv-r.had.co.nz\/Functional-programming.html\">http:\/\/adv-r.had.co.nz\/Functional-programming.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>R\u51fd\u6570\u5f0f\u7f16\u7a0b Functional Programming in R R\u8bed\u8a00\u7684\u5faa\u73af\u5f88\u6162\uff0c\u540c\u65f6\u8bed\u6cd5\u6709\u5f88\u590d\u6742\uff0c\u9677\u9631\u5f88\u591a\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u9010\u884c\u904d\u5386\u4e00\u4e2a\u77e9\u9635\u6709\u4e24\u79cd\u5199\u6cd5\uff1a \uff081\uff09 for (i in seq_len(nrow(m))){print(i)} \uff082\uff09 for (i in 1:nrow(m)){print(i)} \u8fd9\u4e24\u4e2a\u5199\u6cd5\u90a3\u4e2a\u5bf9\uff1f\u6216\u8005\u4e24\u4e2a\u90fd\u5bf9\uff1f \u719f\u6089R\u7684\u7528\u6237\u81ea\u7136\u4f1a\u7528\uff081\uff09\uff0c\u56e0\u4e3a\u5f53m\u77e9\u9635\u7684\u884c\u6570\u4e3a\u96f6\u65f6\uff0c\u53ea\u6709\uff081\uff09\u7684\u7ed3\u679c\u662f\u6b63\u786e\u7684\uff1a &gt; m &lt;- matrix(, nr = 0, nc = 0) &gt; for (i in seq_len(nrow(m))){print(i)} &gt; for (i in 1:nrow(m)){print(i)} &#x5B;1] 1 &#x5B;1] 0 \u4e3a\u4e86\u907f\u514d\u624b\u52a8\u5199\u5faa\u73af\uff0c\u4e00\u79cd\u6298\u4e2d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u7528Filter, Reduce, Map\u7b49\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u6982\u5ff5\u3002 \u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u6bd4\u8f83\u81ea\u7136\u7684\u5904\u7406\u5217\u8868\uff08list\uff09\u6216\u8005\u5411\u91cf\uff08vector\uff09\u7c7b\u578b\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a l &lt;- as.list(seq(9, 1, -1)) d &lt;- data.frame(a = [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[137,45],"class_list":["post-630","post","type-post","status-publish","format-standard","hentry","category-code","tag-functional-programming","tag-r"],"_links":{"self":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/comments?post=630"}],"version-history":[{"count":0,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/630\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/media?parent=630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/categories?post=630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/tags?post=630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}