{"id":506,"date":"2014-03-09T23:31:39","date_gmt":"2014-03-10T03:31:39","guid":{"rendered":"http:\/\/zhanxw.com\/blog\/?p=506"},"modified":"2014-03-09T23:31:39","modified_gmt":"2014-03-10T03:31:39","slug":"r%e7%9a%84%e8%af%a1%e5%bc%82%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/zhanxw.com\/blog\/2014\/03\/r%e7%9a%84%e8%af%a1%e5%bc%82%e9%94%99%e8%af%af\/","title":{"rendered":"R\u7684\u8be1\u5f02\u9519\u8bef"},"content":{"rendered":"<p>A pitfall in R<\/p>\n<p>\u5148\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nfreq &lt;- 0.29\r\naltAllele &lt;- freq * 200 * 2\r\nprint(altAllele) ## this will output 116\r\nprint(length(rep(1, altAllele))) ## what is the output of this??\r\n<\/pre>\n<p>\u6700\u540e\u4e00\u884c\u8f93\u51fa\u7684\u662f115\uff0c \u4e0d\u662f116\u3002\u4e3a\u4ec0\u4e48\uff1f<br \/>\n\u7b54\u6848\u662f\uff1a<\/p>\n<p>altAllele\u662f\u4e00\u4e2anumeric\u7c7b\u578b\u7684\u6570\uff0c\u5c3d\u7ba1\u6253\u5370\u51fa\u6765\u662f116\uff0c\u4f46\u5728\u7535\u8111\u91cc\u5b58\u50a8\u7684\u662f\u4e00\u4e2a\u6bd4\u6574\u6570116\u7a0d\u5c0f\u7684\u6d6e\u70b9\u6570\u3002\u56e0\u6b64rep(1, altAllele)\u662f\u4e00\u4e2a\u957f\u5ea6\u662f115\u7684vector\u3002<\/p>\n<p>R\u7684\u7a0b\u5e8f\u6709\u5f88\u591a\u9677\u9631\u3002\u8fd9\u4e2a\u9677\u9631\u662f\u56e0\u4e3aR\u4e0d\u9700\u8981\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u5b83\u7684\u5e95\u5c42\u8fd8\u662f\u6709\u7c7b\u578b\u7684\u3002\u56e0\u6b64\u5199R\u7a0b\u5e8f\u4e00\u5b9a\u8981\u6ce8\u610f\u8fd9\u70b9\u3002\u89e3\u51b3\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: r; highlight: [2]; title: ; notranslate\" title=\"\">\r\nfreq &lt;- 0.29\r\naltAllele &lt;- as.integer(freq * 200 * 2)\r\nprint(altAllele) ## this will output 116\r\nprint(length(rep(1, altAllele))) ## what is the output of this??\r\n<\/pre>\n<p>Python\u91cc\u6709\u6ca1\u6709\u7c7b\u4f3c\u7684\u9677\u9631\uff1f\u7ed3\u8bba\u662f\u4e00\u6837\u6709 (Python 2.7.5 NumPy 1.8.0)\uff1a<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nfreq = .29\r\naltAllele = freq * 200 * 2\r\nlen(np.repeat(1, altAllele)) ## output 115 here.\r\n<\/pre>\n<p>\u4f46\u662fPython\u91cc\u6253\u5370altAllele\u7684\u503c\u4e0d\u662f116\uff0c\u800c\u662f115.99999999999999\u3002\u8fd9\u603b\u6bd4R\u6253\u5370\u51fa16\u8981\u597d\u4e00\u70b9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A pitfall in R \u5148\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a freq &lt;- 0.29 altAllele &lt;- freq * 200 * 2 print(altAllele) ## this will output 116 print(length(rep(1, altAllele))) ## what is the output of this?? \u6700\u540e\u4e00\u884c\u8f93\u51fa\u7684\u662f115\uff0c \u4e0d\u662f116\u3002\u4e3a\u4ec0\u4e48\uff1f \u7b54\u6848\u662f\uff1a altAllele\u662f\u4e00\u4e2anumeric\u7c7b\u578b\u7684\u6570\uff0c\u5c3d\u7ba1\u6253\u5370\u51fa\u6765\u662f116\uff0c\u4f46\u5728\u7535\u8111\u91cc\u5b58\u50a8\u7684\u662f\u4e00\u4e2a\u6bd4\u6574\u6570116\u7a0d\u5c0f\u7684\u6d6e\u70b9\u6570\u3002\u56e0\u6b64rep(1, altAllele)\u662f\u4e00\u4e2a\u957f\u5ea6\u662f115\u7684vector\u3002 R\u7684\u7a0b\u5e8f\u6709\u5f88\u591a\u9677\u9631\u3002\u8fd9\u4e2a\u9677\u9631\u662f\u56e0\u4e3aR\u4e0d\u9700\u8981\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u5b83\u7684\u5e95\u5c42\u8fd8\u662f\u6709\u7c7b\u578b\u7684\u3002\u56e0\u6b64\u5199R\u7a0b\u5e8f\u4e00\u5b9a\u8981\u6ce8\u610f\u8fd9\u70b9\u3002\u89e3\u51b3\u65b9\u6cd5\u5982\u4e0b\uff1a freq &lt;- 0.29 altAllele &lt;- as.integer(freq * 200 * 2) print(altAllele) ## this will output 116 print(length(rep(1, altAllele))) ## [&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,53,45],"class_list":["post-506","post","type-post","status-publish","format-standard","hentry","category-code","category-statistics","tag-pitfall","tag-python","tag-r"],"_links":{"self":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/506","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=506"}],"version-history":[{"count":0,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/506\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/media?parent=506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/categories?post=506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/tags?post=506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}