{"id":640,"date":"2015-04-01T23:57:37","date_gmt":"2015-04-02T03:57:37","guid":{"rendered":"http:\/\/zhanxw.com\/blog\/?p=640"},"modified":"2015-04-01T23:57:37","modified_gmt":"2015-04-02T03:57:37","slug":"r%e7%9a%84%e8%af%a1%e5%bc%82%e9%94%99%e8%af%af2","status":"publish","type":"post","link":"https:\/\/zhanxw.com\/blog\/2015\/04\/r%e7%9a%84%e8%af%a1%e5%bc%82%e9%94%99%e8%af%af2\/","title":{"rendered":"R\u7684\u8be1\u5f02\u9519\u8bef(2)"},"content":{"rendered":"<p>R\u7684\u8be1\u5f02\u9519\u8bef(2)<br \/>\nA pitfall in R<\/p>\n<p>R\u91cc\u9762\u7684\u9677\u9631\u4e00\u4e2a\u63a5\u4e00\u4e2a\uff0c\u7ee7\u4e0a\u6b21\u8bf4\u7684<a href=\"http:\/\/zhanxw.com\/blog\/2014\/03\/r%e7%9a%84%e8%af%a1%e5%bc%82%e9%94%99%e8%af%af\/\" title=\"R\u7684\u8be1\u5f02\u9519\u8bef\">\u8be1\u5f02\u9519\u8bef<\/a>\uff0c\u6700\u8fd1\u6211\u53c8\u8dcc\u8fdb\u4e86\u53e6\u4e00\u4e2a\u5751\u3002<\/p>\n<p>\u5148\u521b\u9020\u4e00\u4e2a\u53ebdf\u7684data.frame\uff0c \u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\noptions(stringsAsFactors = FALSE)\r\ndf &lt;- data.frame(\r\n  group = c(&quot;group1&quot;, &quot;group2&quot;),\r\n  val = c(100, 9)\r\n  )\r\ndf\r\n\uff03 df content:\r\n\uff03\r\n\uff03   group val\r\n\uff031 group1 100\r\n\uff032 group2   9\r\n<\/pre>\n<p>\u6211\u4eec\u7edf\u8ba1\u4e00\u4e0bval\u5217\u5728\u6bcf\u7ec4\uff08group\uff09\u7684\u5747\u503c\uff1a<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## get mean value per group\r\nlibrary(plyr)\r\nres &lt;- ddply(df, .(group), function(x) {\r\n  c(group = unique(x$group), \r\n    val = mean(x$val))\r\n  })\r\nres\r\n<\/pre>\n<p>res\u7684\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n&gt; res\r\n   group val\r\n1 group1  100\r\n2 group2  9\r\n&gt; res&#x5B;1,2] &gt; res&#x5B;2,2]\r\n&#x5B;1] FALSE\r\n<\/pre>\n<p>\u56f0\u60d1\u7684\u5730\u65b9\u6765\u4e86\uff0cres[1,2]\u663e\u793a\u7684\u662f\u201c100\u201d\uff0c res[2,2]\u73b0\u5b9e\u7684\u662f\u201c9\u201d, \u4e3a\u4ec0\u4e48res[1,2] > res[2,2]\u7684\u7ed3\u679c\u662fFALSE\uff1f<br \/>\nR\u80af\u5b9a\u662f\u8ba4\u4e3a100\u6bd49\u5927\u3002\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002<\/p>\n<p>\u90a3\u4e0a\u9762\u54ea\u4e2a\u5730\u65b9\u6709\u9677\u9631\u5462\uff1f\u518d\u4ed4\u7ec6\u770b\u4e00\u4e0b\u9ad8\u4eae\u663e\u793a\u7684\u4e24\u884c\uff0c\u770b\u51fa\u95ee\u9898\u4e86\u4e48\uff1f<\/p>\n<pre class=\"brush: r; highlight: [3,4]; title: ; notranslate\" title=\"\">\r\n## get mean value per group\r\nres &lt;- ddply(df, .(group), function(x) {\r\n  c(group = unique(x$group), \r\n    val = mean(x$val))\r\n  })\r\n<\/pre>\n<p>\u9677\u9631\u5728\u4e8e\u7b2c3\uff0d4\u884c\uff0c\u5f53\u7528c()\u6765\u6784\u9020\u4e00\u4e2a\u5411\u91cf\u65f6\uff0cval\u88ab\u81ea\u52a8\u4ece\u6570\u503c\u578b\u8f6c\u6210\u5b57\u7b26\u578b\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b58\u50a8\u7684\u662f\u201c100\u201d\u548c\u201c9\u201d\uff0c\u4e0d\u662f100\u548c9\u3002<br \/>\n\u5728\u5b57\u7b26\u578b\u6570\u503c\u7684\u6bd4\u8f83\u4e2d\uff0c\u201c9\u201d\u662f\u4f1a\u6bd4\u201c100\u201d\u5927\u7684\u3002\u56e0\u6b64order\u8f93\u51fa\u7684\u662f2 1.<\/p>\n<p>\u4ece\u8fd9\u4e2a\u5751\u91cc\u722c\u51fa\u6765\uff0c\u4e0d\u7531\u5f97\u5410\u69fd\u4e00\u4e0b\uff0cR\u7684\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362\u80fd\u51cf\u5c11\u7a0b\u5e8f\u7684\u4ee3\u7801\u91cf\uff0c\u4f46\u662f\u8fd9\u79cd\u7c7b\u578b\u8f6c\u6362\u5f88\u5bb9\u6613\u5f15\u5165\u9519\u8bef\u3002<br \/>\n\u4e5f\u5c31\u662f\u8bf4R\u7f16\u7a0b\u65f6\u4e0d\u9700\u8981\u7a0b\u5e8f\u5458\u53bb\u68c0\u67e5\u7c7b\u578b\uff0c\u4f46\u5728\u5e95\u5c42R\u4f1a\u81ea\u52a8\u7684\uff08\u6709\u65f6\u4e5f\u662f\u81ea\u4f5c\u806a\u660e\u7684\uff09\u8f6c\u6362\u7c7b\u578b\u3002<br \/>\n\u8fd9\u5c31\u8981\u6c42\u7a0b\u5e8f\u5458\u989d\u5916\u8d39\u5fc3\u8003\u8651\u7c7b\u578b\u4e4b\u95f4\u7684\u81ea\u52a8\u8f6c\u6362\uff0c\u5b9e\u9645\u4e0a\u52a0\u91cd\u4e86\u7f16\u7a0b\u7684\u8d1f\u62c5\u3002<br \/>\n\u5982\u679c\u4e0d\u7136\uff0c\u7a0b\u5e8f\u5458\u653e\u4efbR\u505a\u81ea\u52a8\u7684\u7c7b\u578b\u5339\u914d\u548c\u8f6c\u6362\uff0c\u5c31\u53ef\u80fd\u5f97\u5230\u9519\u8bef\u7684\u7ed3\u679c\uff0c\u8fd9\u5728\u79d1\u5b66\u7814\u7a76\u4e2d\u5b9e\u5728\u662f\u5f88\u5371\u9669\u3002<\/p>\n<p>\u65e2\u7136R\u7684\u5751\u8fd9\u4e48\u591a\uff0c\u6211\u60f3\u8fc7\u53bb\u8bd5\u8bd5\u65b0\u51fa\u7684Julia\u6216\u8005Go\u8bed\u8a00\u3002\u53ef\u60dc\u73b0\u5728\u8fd9\u4e9b\u8bed\u8a00\u90fd\u6ca1\u6709\u5f88\u597d\u7684\u652f\u6301\u7edf\u8ba1\u8ba1\u7b97\uff0c\u6bd4\u5982Julia\u76ee\u524d\u7f3a\u5c11\u5bf9\u7f3a\u5931\u6570\u636e\uff08missing data\uff09\u7684\u652f\u6301\uff0c<br \/>\n\u6bd4\u8f83\u6709\u5e0c\u671b\u7684NumPy\u5199\u8d77\u6765\u8bed\u6cd5\u7e41\u6742\uff0c\u8fd1\u671f\u5438\u5f15\u4e0d\u5230\u5927\u591a\u6570\u7684\u7edf\u8ba1\u5b66\u5bb6\uff08\u7edf\u8ba1\u7684\u7528\u6237\u592a\u5c11\uff0c\u673a\u5668\u5b66\u4e60\u7684\u7528\u6237\u591a\uff09\u3002<br \/>\n\u8981\u662f\u80fd\u6709\u4e00\u79cd\u8ba1\u7b97\u5de5\u5177\uff0c\u53ef\u4ee5\u8ba9\u7edf\u8ba1\u5b66\u5bb6\u5f88\u5bb9\u6613\u7684\u4f7f\u7528\uff0c\u540c\u65f6\u53c8\u6709\u8f83\u5f3a\u7684\u7c7b\u578b\u7cfb\u7edf\uff0c\u8fd8\u80fd\u628a\u5927\u591a\u6570R\u5305\u8fc1\u79fb\u8fc7\u53bb\uff0c\u90a3\u8fd9\u79cd\u5de5\u5177\u5fc5\u6709\u8fdc\u5927\u524d\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>R\u7684\u8be1\u5f02\u9519\u8bef(2) A pitfall in R R\u91cc\u9762\u7684\u9677\u9631\u4e00\u4e2a\u63a5\u4e00\u4e2a\uff0c\u7ee7\u4e0a\u6b21\u8bf4\u7684\u8be1\u5f02\u9519\u8bef\uff0c\u6700\u8fd1\u6211\u53c8\u8dcc\u8fdb\u4e86\u53e6\u4e00\u4e2a\u5751\u3002 \u5148\u521b\u9020\u4e00\u4e2a\u53ebdf\u7684data.frame\uff0c \u5982\u4e0b\u6240\u793a\uff1a options(stringsAsFactors = FALSE) df &lt;- data.frame( group = c(&quot;group1&quot;, &quot;group2&quot;), val = c(100, 9) ) df \uff03 df content: \uff03 \uff03 group val \uff031 group1 100 \uff032 group2 9 \u6211\u4eec\u7edf\u8ba1\u4e00\u4e0bval\u5217\u5728\u6bcf\u7ec4\uff08group\uff09\u7684\u5747\u503c\uff1a ## get mean value per group library(plyr) res &lt;- ddply(df, .(group), function(x) { c(group = unique(x$group), val [&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,15],"tags":[130,45],"class_list":["post-640","post","type-post","status-publish","format-standard","hentry","category-code","category-statistics","tag-pitfall","tag-r"],"_links":{"self":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/640","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=640"}],"version-history":[{"count":0,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/640\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/media?parent=640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/categories?post=640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/tags?post=640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}