{"id":180274,"date":"2024-05-08T20:32:10","date_gmt":"2024-05-08T12:32:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/180274.html"},"modified":"2024-05-08T20:32:19","modified_gmt":"2024-05-08T12:32:19","slug":"r%e8%af%ad%e8%a8%80%e5%a6%82%e4%bd%95%e6%a0%b9%e6%8d%ae%e8%a1%8c%e5%90%8d%e7%a7%b0%e7%ad%9b%e9%80%89%e9%9c%80%e8%a6%81%e7%9a%84%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/180274.html","title":{"rendered":"R\u8bed\u8a00\u5982\u4f55\u6839\u636e\u884c\u540d\u79f0\u7b5b\u9009\u9700\u8981\u7684\u884c"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27065437\/aae59198-9dee-41fc-a543-f6aec33d3e6a.webp\" alt=\"R\u8bed\u8a00\u5982\u4f55\u6839\u636e\u884c\u540d\u79f0\u7b5b\u9009\u9700\u8981\u7684\u884c\" \/><\/p>\n<p><p>R\u8bed\u8a00\u4e2d\u53ef\u4ee5\u6839\u636e\u884c\u540d\u79f0\u7b5b\u9009\u9700\u8981\u7684\u884c\uff0c\u4e3b\u8981\u901a\u8fc7<strong>\u4f7f\u7528\u884c\u540d\u79f0\u7684\u903b\u8f91\u5339\u914d\u3001\u57fa\u4e8e\u884c\u540d\u79f0\u7684\u5b50\u96c6\u5212\u5206\u3001\u6216\u662f<code>dplyr<\/code>\u5305\u7684\u8f85\u52a9\u51fd\u6570<\/strong>\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u82e5\u6709\u4e00\u4e2a\u6570\u636e\u6846<code>df<\/code>\uff0c\u4e14\u9700\u8981\u7b5b\u9009\u51fa\u884c\u540d\u79f0\u4e3a<code>&quot;row1&quot;<\/code>\u7684\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>df[&quot;row1&quot;, ]<\/code>\u6765\u76f4\u63a5\u6309\u884c\u540d\u7b5b\u9009\u3002\u6b64\u5916\uff0c\u5982\u679c\u8981\u7b5b\u9009\u591a\u4e2a\u7279\u5b9a\u7684\u884c\uff0c\u53ef\u4ee5\u91c7\u7528<code>df[c(&quot;row1&quot;, &quot;row2&quot;), ]<\/code>\u7684\u65b9\u5f0f\u3002<strong>\u5229\u7528<code>dplyr<\/code>\u5305\u7684<code>filter()<\/code>\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u914d\u5408<code>row.names()<\/code>\u51fd\u6570\u7075\u6d3b\u7b5b\u9009\u5177\u6709\u7279\u5b9a\u884c\u540d\u79f0\u7684\u884c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5229\u7528\u884c\u540d\u79f0\u7684\u903b\u8f91\u5339\u914d<\/h3>\n<\/p>\n<p><p><strong>\u903b\u8f91\u5339\u914d<\/strong>\u662f\u6839\u636e\u884c\u540d\u79f0\u8fdb\u884c\u884c\u7b5b\u9009\u7684\u57fa\u7840\u65b9\u5f0f\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u6570\u636e\u6846\u7684\u5b50\u96c6\u4e2d\u901a\u8fc7\u884c\u540d\u79f0\u8fdb\u884c\u7b5b\u9009\u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u6211\u4eec\u5e38\u7ed3\u5408<code>%in%<\/code>\u8fd0\u7b97\u7b26\uff0c\u6765\u7b5b\u9009\u7b26\u5408\u6761\u4ef6\u7684\u591a\u4e2a\u7279\u5b9a\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\"># \u5047\u5b9adf\u4e3a\u6570\u636e\u6846\uff0crows\u4e3a\u9700\u8981\u7b5b\u9009\u7684\u884c\u540d\u5411\u91cf<\/p>\n<p>rows &lt;- c(&quot;row1&quot;, &quot;row2&quot;, &quot;row3&quot;)<\/p>\n<p>selected_rows &lt;- df[row.names(df) %in% rows, ]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>selected_rows<\/code>\u5c06\u4f1a\u5305\u542b\u6240\u6709\u884c\u540d\u79f0\u5728<code>rows<\/code>\u5411\u91cf\u4e2d\u7684\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u57fa\u4e8e\u884c\u540d\u79f0\u7684\u5b50\u96c6\u5212\u5206<\/h3>\n<\/p>\n<p><p><strong>\u5b50\u96c6\u5212\u5206<\/strong>\u53ef\u4ee5\u901a\u8fc7\u884c\u540d\u79f0\u7b5b\u9009\u6570\u636e\uff0c\u7279\u522b\u662f\u5f53\u6570\u636e\u6846\u7684\u884c\u6709\u660e\u786e\u7684\u6807\u7b7e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u884c\u540d\u7d22\u5f15\u6765\u76f4\u63a5\u5212\u5206\u5b50\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\"># \u76f4\u63a5\u901a\u8fc7\u884c\u540d\u79f0\u8bbf\u95ee<\/p>\n<p>specific_row &lt;- df[&quot;row1&quot;, ]<\/p>\n<h2><strong>\u540c\u6837\u53ef\u4ee5\u7b5b\u9009\u591a\u884c<\/strong><\/h2>\n<p>multiple_rows &lt;- df[c(&quot;row1&quot;, &quot;row2&quot;), ]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u901a\u8fc7\u884c\u540d\u8fdb\u884c\u7d22\u5f15\u65f6\uff0c\u82e5\u884c\u540d\u4e0d\u5b58\u5728\uff0c\u8fd4\u56de\u7684\u6570\u636e\u6846\u5c06\u4f1a\u662f\u4e00\u4e2a\u5168\u90e8\u7531<code>NA<\/code>\u6784\u6210\u7684\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>dplyr<\/code>\u5305\u8fdb\u884c\u7b5b\u9009<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>dplyr<\/code>\u5305\u7684<code>filter()<\/code>\u51fd\u6570\u7ed3\u5408<code>row.names()<\/code>\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u66f4\u52a0\u5f3a\u5927\u5e76\u4e14\u8868\u8fbe\u6027\u66f4\u597d\u7684\u884c\u7b5b\u9009\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">library(dplyr)<\/p>\n<h2><strong>\u5229\u7528dplyr\u7684filter()\u51fd\u6570\u548crow.names()\u7b5b\u9009\u6307\u5b9a\u884c\u540d\u79f0\u7684\u884c<\/strong><\/h2>\n<p>filtered_df &lt;- df %&gt;% <\/p>\n<p>  filter(row.names(.) %in% c(&quot;row1&quot;, &quot;row2&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>filter()<\/code>\u51fd\u6570\u80fd\u591f\u4e0e<code>row.names()<\/code>\u534f\u540c\u5de5\u4f5c\uff0c\u4ee5\u9009\u62e9\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u6761\u4ef6\u7b5b\u9009<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u591a\u4e2a\u6761\u4ef6\u6765\u7b5b\u9009\u884c\uff0c\u53ef\u4ee5\u66f4\u7ec6\u81f4\u5730\u63a7\u5236\u7b5b\u9009\u8fc7\u7a0b\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u903b\u8f91\u8fd0\u7b97\u7b26(\u5982<code>&amp;<\/code>\u548c<code>|<\/code>)\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\"># \u7ed3\u5408\u591a\u4e2a\u6761\u4ef6\u8fdb\u884c\u7b5b\u9009<\/p>\n<p>filtered_rows &lt;- df[row.names(df) %in% c(&quot;row1&quot;, &quot;row2&quot;) &amp; df$someColumn &gt; value, ]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u9009\u51fa\u540c\u65f6\u6ee1\u8db3\u201c\u884c\u540d\u4e3a<code>row1<\/code>\u6216<code>row2<\/code>\u201d\u548c\u201c\u67d0\u5217\u7684\u503c\u5927\u4e8e<code>value<\/code>\u201d\u7684\u6240\u6709\u884c\u3002<\/p>\n<\/p>\n<p><p>\u5728R\u8bed\u8a00\u4e2d\uff0c<strong>\u884c\u540d\u79f0\u7b5b\u9009\u662f\u6570\u636e\u64cd\u4f5c\u4e2d\u7684\u4e00\u4e2a\u5e38\u7528\u624b\u6bb5<\/strong>\u3002\u638c\u63e1\u4e0d\u540c\u7684\u7b5b\u9009\u6280\u5de7\u80fd\u591f\u5e2e\u52a9\u7528\u6237\u66f4\u52a0\u65b9\u4fbf\u5730\u5904\u7406\u6570\u636e\u96c6\u3002\u4e0a\u8ff0\u65b9\u6cd5\u5404\u6709\u9002\u7528\u573a\u666f\uff0c\u6839\u636e\u4e0d\u540c\u7684\u6570\u636e\u5904\u7406\u9700\u6c42\u548c\u4e2a\u4eba\u504f\u597d\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528R\u8bed\u8a00\u6839\u636e\u884c\u540d\u79f0\u8fdb\u884c\u7b5b\u9009\uff1f<\/strong><\/p>\n<p>\u5728R\u8bed\u8a00\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570<code>subset()<\/code>\u6216\u8005\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\u8fdb\u884c\u884c\u7b5b\u9009\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u6307\u5b9a\u8981\u7b5b\u9009\u7684\u6570\u636e\u6846\u6216\u77e9\u9635\uff0c\u7136\u540e\u4f7f\u7528\u903b\u8f91\u8868\u8fbe\u5f0f\u6765\u9009\u62e9\u9700\u8981\u7684\u884c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26&quot;==&quot;\u6765\u7b5b\u9009\u884c\u540d\u79f0\u7b49\u4e8e\u7279\u5b9a\u503c\u7684\u884c\u3002<\/p>\n<p><strong>2. R\u8bed\u8a00\u4e2d\u5982\u4f55\u6839\u636e\u884c\u540d\u79f0\u7684\u90e8\u5206\u5b57\u7b26\u4e32\u8fdb\u884c\u7b5b\u9009\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u60f3\u6839\u636e\u884c\u540d\u79f0\u7684\u90e8\u5206\u5b57\u7b26\u4e32\u8fdb\u884c\u7b5b\u9009\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570<code>grepl()<\/code>\u6216\u8005<code>grep()<\/code>\u6765\u5b9e\u73b0\u3002<code>grepl()<\/code>\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u903b\u8f91\u5411\u91cf\uff0c\u6307\u793a\u54ea\u4e9b\u884c\u540d\u79f0\u5305\u542b\u4e86\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u3002<code>grep()<\/code>\u51fd\u6570\u5219\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u5411\u91cf\uff0c\u6307\u793a\u5305\u542b\u6307\u5b9a\u5b57\u7b26\u4e32\u7684\u884c\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<p><strong>3. R\u8bed\u8a00\u4e2d\u5982\u4f55\u6839\u636e\u591a\u4e2a\u884c\u540d\u79f0\u8fdb\u884c\u7b5b\u9009\uff1f<\/strong><\/p>\n<p>\u8981\u6839\u636e\u591a\u4e2a\u884c\u540d\u79f0\u8fdb\u884c\u7b5b\u9009\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26&quot;|&quot;\uff08\u6216\uff09\u6216&quot;&amp;&quot;\uff08\u548c\uff09\u6765\u7ec4\u5408\u591a\u4e2a\u7b5b\u9009\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>subset()<\/code>\u51fd\u6570\u6765\u7b5b\u9009\u5177\u6709\u67d0\u4e9b\u884c\u540d\u79f0\u7684\u884c\uff0c\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26&quot;|&quot;\u5c06\u6761\u4ef6\u8fde\u63a5\u8d77\u6765\u3002\u53e6\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>match()<\/code>\u51fd\u6570\u6765\u7b5b\u9009\u5177\u6709\u6307\u5b9a\u884c\u540d\u79f0\u7684\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"R\u8bed\u8a00\u4e2d\u53ef\u4ee5\u6839\u636e\u884c\u540d\u79f0\u7b5b\u9009\u9700\u8981\u7684\u884c\uff0c\u4e3b\u8981\u901a\u8fc7\u4f7f\u7528\u884c\u540d\u79f0\u7684\u903b\u8f91\u5339\u914d\u3001\u57fa\u4e8e\u884c\u540d\u79f0\u7684\u5b50\u96c6\u5212\u5206\u3001\u6216\u662fdplyr\u5305\u7684\u8f85\u52a9 [&hellip;]","protected":false},"author":3,"featured_media":180289,"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\/180274"}],"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=180274"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180274\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/180289"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=180274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=180274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=180274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}