{"id":480,"date":"2013-10-18T22:31:08","date_gmt":"2013-10-19T02:31:08","guid":{"rendered":"http:\/\/zhanxw.com\/blog\/?p=480"},"modified":"2013-10-18T22:31:08","modified_gmt":"2013-10-19T02:31:08","slug":"r%e8%af%ad%e8%a8%80%e5%a6%82%e4%bd%95%e7%94%a8%e7%b4%a2%e5%bc%95","status":"publish","type":"post","link":"https:\/\/zhanxw.com\/blog\/2013\/10\/r%e8%af%ad%e8%a8%80%e5%a6%82%e4%bd%95%e7%94%a8%e7%b4%a2%e5%bc%95\/","title":{"rendered":"R\u8bed\u8a00\u5982\u4f55\u7528\u7d22\u5f15"},"content":{"rendered":"<p>R\u8bed\u8a00\u5982\u4f55\u7528\u7d22\u5f15<br \/>\nHow to use index in R<\/p>\n<p>R\u8bed\u8a00\u6709\u4e09\u79cd\u7d22\u5f15\u65b9\u5f0f\uff1a [[, [, $<br \/>\n\u672c\u6587\u57fa\u4e8e<a href=\"http:\/\/cran.r-project.org\/doc\/manuals\/r-release\/R-lang.html#Indexing\" title=\"Indexing\">R\u8bed\u8a00\u7684\u5b9a\u4e49<\/a>\uff0c\u7528\u4f8b\u5b50\u91cc\u8bf4\u660e\u8fd9\u4e09\u79cd\u7d22\u5f15\u65b9\u5f0f\u7684\u4f7f\u7528\u6761\u4ef6\u548c\u5b83\u4eec\u4e4b\u95f4\u7684\u7ec6\u5fae\u5dee\u522b\u3002<\/p>\n<p>\u9996\u5148\uff0cR\u91cc\u9762\u6709\u4e0b\u9762\u7684\u7d22\u5f15\u65b9\u5f0f\uff1a<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nx&#x5B;i]\r\nx&#x5B;i, j]\r\nx&#x5B;&#x5B;i]]\r\nx&#x5B;&#x5B;i, j]]\r\nx$a\r\nx$&quot;a&quot;\r\n<\/pre>\n<p>\u603b\u7ed3\u5e76\u7ffb\u8bd1\u4e00\u4e0b\uff1a<\/p>\n<p>1. \u5982\u679cx\u662f\u4e00\u4e2avector (\u5305\u62ecmatrix\u548carray)\uff0c\u7528[i]\u6216\u8005[[i]]\u5f97\u5230\u7684\u662f\u7b2ci\u4e2a\u5143\u7d20\u3002\u800c\u4e14\u4e00\u822c\u7528[\u800c\u4e0d\u662f[[\uff0c\u7528[[\u7684\u8bdd\u4f1a\u5931\u53bb\u53d8\u91cf\u7684\u540d\u5b57\uff08name\u548cdimnames\uff09\u3002\u6bd4\u5982\uff1a<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\naVec &lt;- c(1,2,3)\r\nnames(aVec) &lt;- c(&quot;x1&quot;, &quot;x2&quot;, &quot;x3&quot;)\r\n&gt; aVec&#x5B;1]\r\nx1 \r\n 1 \r\n&gt; aVec&#x5B;&#x5B;1]]\r\n&#x5B;1] 1\r\n<\/pre>\n<p>2. \u5982\u679cx\u662f\u4e00\u4e2alist\uff0c\u90a3\u4e48[i]\u5f97\u5230\u7684\u662f\u4e00\u4e2alist\uff0c\u7528[[i]]\u5f97\u5230\u7684\u662f\u7b2ci\u4e2a\u5143\u7d20\u3002aList[1]\u5f97\u5230\u7684\u662f\u4e00\u4e2alist\uff0caList[[1]]\u5f97\u5230\u7684\u4e00\u4e2avector\u3002<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\naList &lt;- list(1, 2)\r\n&gt; aList&#x5B;1]\r\n$x1\r\n&#x5B;1] 1\r\n\r\n&gt; aList&#x5B;&#x5B;1]]\r\n&#x5B;1] 1\r\n<\/pre>\n<p>3. \u5982\u679c\u7d22\u5f15\u662f\u4e00\u4e2avector\uff08\u6bd4\u5982\u4e00\u6b21\u9009\u53d6\u591a\u4e2a\u5143\u7d20\uff09\uff0c\u53ea\u80fd\u7528[\uff0c\u4e0d\u80fd\u7528[[\u3002\u9009\u51fa\u6765\u7684\u5143\u7d20\u7c7b\u578b\uff0c\u7d22\u5f15\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u662f\u4e00\u6837\u6548\u679c\u3002<\/p>\n<p>4. \u7528$\u52a0\u4e00\u4e2a\u53d8\u91cf\u6765\u7d22\u5f15list\uff0c\u8fd9\u4e2a\u53d8\u91cf\u4f1a\u6309\u5b57\u9762\u9009\u53d6\uff0c\u4e0d\u4f1a\u5148\u6c42\u503c\u518d\u9009\u53d6\u3002\u6bd4\u5982\uff1a x<-1; aList[[x]] \u548c aList[[1]]\u4e0d\u4e00\u6837\u3002\n\n5. \u7528$\u6765\u7d22\u5f15list\u4e4b\u5916\u7684\u5143\u7d20\uff0c\u5f97\u5230\u7684\u662fNULL\n\n\u5173\u4e8e\u5404\u79cdindex\u7684\u6d4b\u8bd5\uff0c\u8fd8\u53ef\u4ee5\u53c2\u8003\u8fd9\u4e2a<a href=\"http:\/\/rpubs.com\/zhanxw\/9792\" title=\"R Indexing\">\u94fe\u63a5<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>R\u8bed\u8a00\u5982\u4f55\u7528\u7d22\u5f15 How to use index in R R\u8bed\u8a00\u6709\u4e09\u79cd\u7d22\u5f15\u65b9\u5f0f\uff1a [[, [, $ \u672c\u6587\u57fa\u4e8eR\u8bed\u8a00\u7684\u5b9a\u4e49\uff0c\u7528\u4f8b\u5b50\u91cc\u8bf4\u660e\u8fd9\u4e09\u79cd\u7d22\u5f15\u65b9\u5f0f\u7684\u4f7f\u7528\u6761\u4ef6\u548c\u5b83\u4eec\u4e4b\u95f4\u7684\u7ec6\u5fae\u5dee\u522b\u3002 \u9996\u5148\uff0cR\u91cc\u9762\u6709\u4e0b\u9762\u7684\u7d22\u5f15\u65b9\u5f0f\uff1a x&#x5B;i] x&#x5B;i, j] x&#x5B;&#x5B;i]] x&#x5B;&#x5B;i, j]] x$a x$&quot;a&quot; \u603b\u7ed3\u5e76\u7ffb\u8bd1\u4e00\u4e0b\uff1a 1. \u5982\u679cx\u662f\u4e00\u4e2avector (\u5305\u62ecmatrix\u548carray)\uff0c\u7528[i]\u6216\u8005[[i]]\u5f97\u5230\u7684\u662f\u7b2ci\u4e2a\u5143\u7d20\u3002\u800c\u4e14\u4e00\u822c\u7528[\u800c\u4e0d\u662f[[\uff0c\u7528[[\u7684\u8bdd\u4f1a\u5931\u53bb\u53d8\u91cf\u7684\u540d\u5b57\uff08name\u548cdimnames\uff09\u3002\u6bd4\u5982\uff1a aVec &lt;- c(1,2,3) names(aVec) &lt;- c(&quot;x1&quot;, &quot;x2&quot;, &quot;x3&quot;) &gt; aVec&#x5B;1] x1 1 &gt; aVec&#x5B;&#x5B;1]] &#x5B;1] 1 2. \u5982\u679cx\u662f\u4e00\u4e2alist\uff0c\u90a3\u4e48[i]\u5f97\u5230\u7684\u662f\u4e00\u4e2alist\uff0c\u7528[[i]]\u5f97\u5230\u7684\u662f\u7b2ci\u4e2a\u5143\u7d20\u3002aList[1]\u5f97\u5230\u7684\u662f\u4e00\u4e2alist\uff0caList[[1]]\u5f97\u5230\u7684\u4e00\u4e2avector\u3002 aList &lt;- list(1, 2) &gt; aList&#x5B;1] $x1 &#x5B;1] 1 &gt; aList&#x5B;&#x5B;1]] &#x5B;1] [&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":[124,45],"class_list":["post-480","post","type-post","status-publish","format-standard","hentry","category-code","tag-indexing","tag-r"],"_links":{"self":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/480","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=480"}],"version-history":[{"count":0,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/480\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/media?parent=480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/categories?post=480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/tags?post=480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}