{"id":180764,"date":"2024-05-08T20:42:00","date_gmt":"2024-05-08T12:42:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/180764.html"},"modified":"2024-05-08T20:42:07","modified_gmt":"2024-05-08T12:42:07","slug":"r%e8%af%ad%e8%a8%80%e4%b8%ad%e4%b8%a4%e4%b8%aa%e6%8b%ac%e5%8f%b7%e8%bf%9e%e7%94%a8%ef%bc%8c%e4%be%8b%e5%a6%82dimnamesa1i%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/180764.html","title":{"rendered":"R\u8bed\u8a00\u4e2d\u4e24\u4e2a\u62ec\u53f7\u8fde\u7528\uff0c\u4f8b\u5982dimnames(a)[[1]][i]\u4ec0\u4e48\u610f\u601d"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27070109\/6c35ed53-82c7-40f7-9f58-549f5a1efc67.webp\" alt=\"R\u8bed\u8a00\u4e2d\u4e24\u4e2a\u62ec\u53f7\u8fde\u7528\uff0c\u4f8b\u5982dimnames(a)[[1]][i]\u4ec0\u4e48\u610f\u601d\" \/><\/p>\n<p><p>\u5728R\u8bed\u8a00\u4e2d\uff0c\u5f53\u4f60\u770b\u5230\u4e24\u4e2a\u62ec\u53f7\u8fde\u7528\uff0c\u4f8b\u5982<code>dimnames(a)[[1]][i]<\/code>\uff0c\u8fd9\u610f\u5473\u7740\u6b63\u5728\u8fdb\u884c\u4e24\u4e2a\u4e0d\u540c\u7ea7\u522b\u7684\u63d0\u53d6\u64cd\u4f5c\u3002<strong>\u9996\u5148\uff0c<code>[[ ]]<\/code>\u7528\u4e8e\u4ece\u5217\u8868\u4e2d\u63d0\u53d6\u5143\u7d20\u3001\u5176\u6b21\uff0c<code>[ ]<\/code>\u7528\u4e8e\u63d0\u53d6\u5411\u91cf\u6216\u77e9\u9635\u4e2d\u7684\u5143\u7d20<\/strong>\u3002\u5728\u8fd9\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u4e2d\uff0c<code>dimnames(a)[[1]]<\/code>\u9996\u5148\u4ece\u5bf9\u8c61<code>a<\/code>\u7684\u7ef4\u5ea6\u540d\u79f0\u4e2d\u63d0\u53d6\u7b2c\u4e00\u7ec4\u540d\u79f0\uff0c\u8fd9\u901a\u5e38\u662f\u884c\u540d\u79f0\u3002\u63a5\u7740\uff0c<code>[i]<\/code>\u4ece\u8fd9\u4e9b\u884c\u540d\u79f0\u4e2d\u63d0\u53d6\u7b2c<code>i<\/code>\u4e2a\u540d\u79f0\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u8fd9\u6bb5\u4ee3\u7801\u7528\u4e8e\u83b7\u53d6\u5bf9\u8c61<code>a<\/code>\u7684\u7b2c<code>i<\/code>\u884c\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><p><strong>\u8fdb\u4e00\u6b65\u5c55\u5f00\u63cf\u8ff0<\/strong>\uff1a<code>dimnames<\/code>\u51fd\u6570\u5728R\u8bed\u8a00\u4e2d\u88ab\u7528\u6765\u83b7\u53d6\u6216\u8bbe\u7f6e\u5bf9\u8c61\u7684\u7ef4\u5ea6\u540d\u79f0\uff0c\u6bd4\u5982\u77e9\u9635\u7684\u884c\u548c\u5217\u540d\u3002\u5728<code>dimnames(a)[[1]][i]<\/code>\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e2d\uff0c<code>dimnames(a)<\/code>\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u8fd9\u4e2a\u5217\u8868\u5305\u542b\u4e24\u4e2a\u5143\u7d20\uff08\u5982\u679c<code>a<\/code>\u662f\u4e00\u4e2a\u77e9\u9635\u7684\u8bdd\uff09\uff1a\u7b2c\u4e00\u4e2a\u5143\u7d20\u5305\u542b\u884c\u540d\u79f0\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u5305\u542b\u5217\u540d\u79f0\u3002<code>[[1]]<\/code>\u8fd9\u90e8\u5206\u64cd\u4f5c\u662f\u4ece\u8fd9\u4e2a\u5217\u8868\u4e2d\u7cbe\u786e\u63d0\u53d6\u51fa\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u5373\u6240\u6709\u884c\u7684\u540d\u79f0\u3002\u968f\u540e\uff0c<code>[i]<\/code>\u8fd9\u90e8\u5206\u64cd\u4f5c\u5c31\u662f\u4ece\u8fd9\u4e9b\u884c\u540d\u79f0\u4e2d\u8fdb\u4e00\u6b65\u63d0\u53d6\u51fa\u7b2c<code>i<\/code>\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7ef4\u5ea6\u540d\u79f0\u548c\u5b83\u4eec\u7684\u91cd\u8981\u6027<\/h3>\n<\/p>\n<p><p>\u5728R\u8bed\u8a00\u4e2d\u5904\u7406\u4e8c\u7ef4\u7ed3\u6784\uff0c\u5982\u77e9\u9635\u548c\u6570\u636e\u5e27\u65f6\uff0c\u660e\u786e\u5730\u7ed9\u884c\u548c\u5217\u547d\u540d\u662f\u4e00\u79cd\u826f\u597d\u7684\u5b9e\u8df5\u3002\u8fd9\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u8bbe\u7f6e\u7ef4\u5ea6\u540d\u79f0<\/strong>: \u4f7f\u7528<code>dimnames<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u8bbe\u7f6e\u77e9\u9635\u6216\u6570\u636e\u5e27\u7684\u884c\u548c\u5217\u540d\u79f0\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u77e9\u9635<code>m<\/code>\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>dimnames(m) &lt;- list(c(&quot;row1&quot;, &quot;row2&quot;), c(&quot;col1&quot;, &quot;col2&quot;))<\/code>\u6765\u5206\u522b\u8bbe\u7f6e\u884c\u548c\u5217\u7684\u540d\u79f0\u3002<\/li>\n<li><strong>\u8bbf\u95ee\u7ef4\u5ea6\u540d\u79f0<\/strong>: \u5982\u540c\u4e0a\u9762\u6240\u793a\uff0c\u901a\u8fc7<code>dimnames()<\/code>\u51fd\u6570\u548c\u9002\u5f53\u7684\u4e0b\u6807\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u77e9\u9635\u6216\u6570\u636e\u5e27\u7684\u884c\u548c\u5217\u540d\u79f0\u3002\u8fd9\u5bf9\u4e8e\u7f16\u7a0b\u65f6\u52a8\u6001\u8bbf\u95ee\u8fd9\u4e9b\u540d\u79f0\u975e\u5e38\u91cd\u8981\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u5217\u8868\u548c\u5b83\u4eec\u7684\u7d22\u5f15\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>R\u8bed\u8a00\u7684\u4e00\u4e2a\u6838\u5fc3\u6982\u5ff5\u662f\u5217\u8868\uff08list\uff09\uff0c\u5217\u8868\u662f\u4e00\u79cd\u53ef\u4ee5\u5305\u542b\u4efb\u610f\u7c7b\u578b\u6570\u636e\u7684\u590d\u5408\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5217\u8868\u7684\u57fa\u672c\u4f7f\u7528<\/strong>: \u5217\u8868\u53ef\u4ee5\u901a\u8fc7<code>list()<\/code>\u51fd\u6570\u521b\u5efa\uff0c\u5e76\u53ef\u4ee5\u5305\u542b\u6570\u503c\u3001\u5b57\u7b26\u3001\u5411\u91cf\u4e43\u81f3\u5176\u4ed6\u5217\u8868\u7684\u4efb\u610f\u7ec4\u5408\u3002\u4f8b\u5982\uff0c<code>myList &lt;- list(name=&quot;John&quot;, scores=c(90, 80, 70), det<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ls=list(age=25, gender=&quot;M&quot;))<\/code>\u3002<\/li>\n<li><strong>\u4ece\u5217\u8868\u4e2d\u63d0\u53d6\u5143\u7d20<\/strong>: \u63d0\u53d6\u5217\u8868\u5143\u7d20\u4e3b\u8981\u4f7f\u7528\u4e24\u79cd\u7c7b\u578b\u7684\u62ec\u53f7<code>[[]]<\/code>\u548c<code>[]<\/code>\u3002<code>[[]]<\/code>\u7528\u4e8e\u63d0\u53d6\u5217\u8868\u4e2d\u7684\u5355\u4e2a\u5143\u7d20\uff0c\u800c<code>[]<\/code>\u5219\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c<code>myList[[&quot;name&quot;]]<\/code>\u5c06\u8fd4\u56de&quot;John&quot;\uff0c\u800c<code>myList[&quot;name&quot;]<\/code>\u5c06\u8fd4\u56de\u4e00\u4e2a\u4ec5\u5305\u542bname\u7684\u65b0\u5217\u8868\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u5411\u91cf\u548c\u77e9\u9635\u63d0\u53d6<\/h3>\n<\/p>\n<p><p>\u548c\u5217\u8868\u4e00\u6837\uff0c\u5411\u91cf\u548c\u77e9\u9635\u4e5f\u662fR\u4e2d\u7684\u57fa\u672c\u6570\u636e\u7ed3\u6784\u3002\u5b83\u4eec\u7528\u4e8e\u5b58\u50a8\u548c\u64cd\u4f5c\u6570\u503c\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5411\u91cf\u63d0\u53d6<\/strong>: \u4f7f\u7528\u5355\u4e2a\u62ec\u53f7<code>[ ]<\/code>\u53ef\u4ee5\u8bbf\u95ee\u5411\u91cf\u7684\u7279\u5b9a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c<code>v &lt;- c(1,2,3,4)<\/code>\uff0c\u90a3\u4e48<code>v[2]<\/code>\u5c06\u8fd4\u56de2\u3002<\/li>\n<li><strong>\u77e9\u9635\u63d0\u53d6<\/strong>: \u77e9\u9635\u63d0\u53d6\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\uff0c\u4e00\u79cd\u662f\u4f7f\u7528\u5355\u884c\u548c\u5217\u4e0b\u6807\u5982<code>m[1, 2]<\/code>\uff0c\u53e6\u4e00\u79cd\u662f\u4f7f\u7528\u884c\u6216\u5217\u540d\u79f0\uff08\u5982\u679c\u5b83\u4eec\u5df2\u7ecf\u88ab\u5b9a\u4e49\uff09\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u540d\u4e3a<code>scoreMatrix<\/code>\u7684\u77e9\u9635\uff0c\u5176\u4e2d\u5305\u542b\u5b66\u751f\u7684\u5206\u6570\uff0c<code>scoreMatrix[&quot;John&quot;, &quot;Math&quot;]<\/code>\u5c06\u8fd4\u56deJohn\u5728\u6570\u5b66\u79d1\u76ee\u7684\u5206\u6570\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u5728R\u8bed\u8a00\u4e2d\u8fdb\u884c\u9ad8\u7ea7\u6570\u636e\u63d0\u53d6<\/h3>\n<\/p>\n<p><p>\u5408\u7406\u5730\u4f7f\u7528\u6570\u636e\u63d0\u53d6\u6280\u5de7\u80fd\u591f\u6781\u5927\u5730\u589e\u5f3a\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u8868\u73b0\u529b\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5e94\u7528<code>apply<\/code>\u51fd\u6570\u65cf<\/strong>: R\u8bed\u8a00\u7684<code>apply<\/code>\u51fd\u6570\u65cf\u5141\u8bb8\u4f60\u5bf9\u77e9\u9635\u3001\u6570\u636e\u5e27\u6216\u5217\u8868\u7684\u7ef4\u5ea6\uff08\u884c\u6216\u5217\uff09\u5e94\u7528\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>apply(x, MARGIN, FUN)<\/code>\uff0c\u5176\u4e2d<code>MARGIN<\/code>\u4ee3\u8868\u8981\u5e94\u7528\u51fd\u6570\u7684\u7ef4\u5ea6\uff081\u4e3a\u884c\uff0c2\u4e3a\u5217\uff09\uff0c<code>FUN<\/code>\u662f\u8981\u5e94\u7528\u7684\u51fd\u6570\u3002<\/li>\n<li><strong>\u5229\u7528<code>dplyr<\/code>\u5305\u8fdb\u884c\u6570\u636e\u64cd\u4f5c<\/strong>: <code>dplyr<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u53d8\u5f62\u5305\uff0c\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\u4ee5\u7b80\u5316\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c<code>filter()<\/code>\u51fd\u6570\u53ef\u4ee5\u57fa\u4e8e\u6761\u4ef6\u7b5b\u9009\u6570\u636e\u884c\uff0c\u800c<code>select()<\/code>\u51fd\u6570\u53ef\u4ee5\u9009\u62e9\u6570\u636e\u7684\u7279\u5b9a\u5217\u3002<\/li>\n<\/ul>\n<p><p>\u7efc\u4e0a\uff0c<code>dimnames(a)[[1]][i]<\/code>\u8fd9\u6837\u7684\u8868\u8fbe\u5f0f\u4f53\u73b0\u4e86R\u8bed\u8a00\u5728\u6570\u636e\u64cd\u4f5c\u548c\u63d0\u53d6\u65b9\u9762\u7684\u5f3a\u5927\u80fd\u529b\uff0c\u540c\u65f6\u4e5f\u7a81\u663e\u4e86\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u57fa\u672c\u6982\u5ff5\u548c\u6280\u5de7\u7684\u91cd\u8981\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1: R\u8bed\u8a00\u4e2d\uff0c\u8fde\u7eed\u4f7f\u7528\u4e24\u4e2a\u62ec\u53f7\u7684\u8bed\u6cd5\u5e38\u89c1\u4e8e\u54ea\u4e9b\u60c5\u51b5\uff1f<\/strong><br \/>\n\u5728R\u8bed\u8a00\u4e2d\uff0c\u8fde\u7eed\u4f7f\u7528\u4e24\u4e2a\u62ec\u53f7\u7684\u8bed\u6cd5\u5e38\u89c1\u4e8e\u5904\u7406\u591a\u7ef4\u6570\u636e\u7ed3\u6784\u6216\u83b7\u53d6\u5d4c\u5957\u5bf9\u8c61\u7684\u7279\u5b9a\u5143\u7d20\u65f6\u3002\u6bd4\u5982\uff0c\u4f7f\u7528<code>dimnames(a)[[1]][i]<\/code>\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u77e9\u9635\u6216\u6570\u7ec4<code>a<\/code>\u7684\u7b2c\u4e00\u7ef4\u5ea6\u7684\u5217\u540d\u4e2d\u7684\u7b2c<code>i<\/code>\u4e2a\u5143\u7d20\u3002<\/p>\n<p><strong>Q2: \u5982\u4f55\u89e3\u8bfbR\u8bed\u8a00\u4e2d\u8fde\u7eed\u4f7f\u7528\u4e24\u4e2a\u62ec\u53f7\u6765\u83b7\u53d6\u7279\u5b9a\u5143\u7d20\u7684\u8bed\u6cd5\uff1f<\/strong><br \/>\nR\u8bed\u8a00\u4e2d\u8fde\u7eed\u4f7f\u7528\u4e24\u4e2a\u62ec\u53f7\u7684\u8bed\u6cd5\u53ef\u4ee5\u88ab\u770b\u4f5c\u662f\u4e24\u4e2a\u64cd\u4f5c\u7b26\u7684\u7ec4\u5408\u3002\u7b2c\u4e00\u4e2a\u62ec\u53f7\u7528\u4e8e\u7d22\u5f15\u6216\u83b7\u53d6\u67d0\u4e2a\u5bf9\u8c61\u7684\u7279\u5b9a\u5143\u7d20\uff0c\u800c\u7b2c\u4e8c\u4e2a\u62ec\u53f7\u7528\u4e8e\u8fdb\u4e00\u6b65\u7d22\u5f15\u6216\u83b7\u53d6\u8be5\u5143\u7d20\u7684\u5b50\u5143\u7d20\u3002<\/p>\n<p><strong>Q3: \u5982\u4f55\u7406\u89e3<code>dimnames(a)[[1]][i]<\/code>\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u7684\u5177\u4f53\u542b\u4e49\uff1f<\/strong><br \/>\n\u5728R\u4e2d\uff0c<code>dimnames()<\/code>\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u6216\u8bbe\u7f6e\u4e00\u4e2a\u5bf9\u8c61\u7684\u7ef4\u5ea6\u540d\u79f0\u3002\u5728\u8868\u8fbe\u5f0f<code>dimnames(a)[[1]][i]<\/code>\u4e2d\uff0c<code>a<\/code>\u4ee3\u8868\u4e00\u4e2a\u77e9\u9635\u6216\u6570\u7ec4\uff0c<code>dimnames(a)<\/code>\u8fd4\u56de<code>a<\/code>\u5bf9\u8c61\u7684\u7ef4\u5ea6\u540d\u79f0\uff0c<code>[[1]]<\/code>\u8868\u793a\u53d6\u7b2c\u4e00\u7ef4\u5ea6\u7684\u540d\u79f0\uff0c\u800c<code>[i]<\/code>\u8868\u793a\u53d6\u7b2c\u4e00\u7ef4\u5ea6\u540d\u79f0\u4e2d\u7684\u7b2c<code>i<\/code>\u4e2a\u5143\u7d20\u3002\u56e0\u6b64\uff0c\u8be5\u8868\u8fbe\u5f0f\u7684\u542b\u4e49\u662f\u83b7\u53d6\u77e9\u9635\u6216\u6570\u7ec4<code>a<\/code>\u7684\u7b2c\u4e00\u7ef4\u5ea6\u7684\u5217\u540d\u4e2d\u7684\u7b2c<code>i<\/code>\u4e2a\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728R\u8bed\u8a00\u4e2d\uff0c\u5f53\u4f60\u770b\u5230\u4e24\u4e2a\u62ec\u53f7\u8fde\u7528\uff0c\u4f8b\u5982dimnames(a)[[1]][i]\uff0c\u8fd9\u610f\u5473\u7740\u6b63\u5728\u8fdb\u884c\u4e24\u4e2a\u4e0d\u540c\u7ea7\u522b\u7684\u63d0 [&hellip;]","protected":false},"author":3,"featured_media":180771,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180764"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=180764"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180764\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/180771"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=180764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=180764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=180764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}