{"id":180205,"date":"2024-05-08T20:30:28","date_gmt":"2024-05-08T12:30:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/180205.html"},"modified":"2024-05-08T20:30:33","modified_gmt":"2024-05-08T12:30:33","slug":"%e5%a6%82%e4%bd%95%e7%94%a8r%e8%af%ad%e8%a8%80%e5%8c%b9%e9%85%8d%e4%b8%a4%e4%b8%aa%e8%a1%a8%e7%9a%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/180205.html","title":{"rendered":"\u5982\u4f55\u7528R\u8bed\u8a00\u5339\u914d\u4e24\u4e2a\u8868\u7684\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27065315\/7345d46d-55db-489b-b848-7631224f2ef9.webp\" alt=\"\u5982\u4f55\u7528R\u8bed\u8a00\u5339\u914d\u4e24\u4e2a\u8868\u7684\u6570\u636e\" \/><\/p>\n<p><p><strong>\u7528R\u8bed\u8a00\u5339\u914d\u4e24\u4e2a\u8868\u7684\u6570\u636e<\/strong> \u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684\u51fd\u6570\u5b9e\u73b0\uff0c\u5982 <code>merge()<\/code>\u3001<code>match()<\/code>\u3001<code>inner_join()<\/code>\uff08\u6765\u81eadplyr\u5305\uff09\u3001<code>left_join()<\/code>\u7b49\u3002\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u4e00\u4e2a\u6216\u591a\u4e2a\u952e\u5c06\u4e24\u4e2a\u6570\u636e\u96c6\u5408\u5e76\u4e3a\u4e00\u4e2a\uff0c<strong>\u6838\u5fc3\u5728\u4e8e\u786e\u5b9a\u8fde\u63a5\u952e\u548c\u8fde\u63a5\u7c7b\u578b<\/strong>\u3002\u4f8b\u5982\uff0c<code>merge()<\/code> \u51fd\u6570\u5141\u8bb8\u7528\u6237\u6307\u5b9a\u67d0\u4e9b\u5217\u4f5c\u4e3a\u8fde\u63a5\u952e\uff0c\u6839\u636e\u8fd9\u4e9b\u952e\u5c06\u4e24\u4e2a\u6570\u636e\u6846\uff08data.frames\uff09\u4e2d\u7684\u76f8\u5173\u884c\u914d\u5bf9\u8d77\u6765\u3002\u6b64\u5916\uff0c<code>inner_join()<\/code> \u51fd\u6570\u901a\u5e38\u7528\u4e8e\u4fdd\u7559\u4e24\u4e2a\u6570\u636e\u6846\u4e2d\u90fd\u5b58\u5728\u7684\u884c\uff0c\u4ece\u800c\u5b9e\u73b0\u7cbe\u786e\u5339\u914d\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5339\u914d\u4e24\u4e2a\u6570\u636e\u8868\u65f6\uff0c\u9700\u8981\u660e\u786e\u5339\u914d\u7684\u903b\u8f91\uff0c\u5373\u53ea\u4fdd\u7559\u5339\u914d\u5230\u7684\u884c\uff08\u5185\u8fde\u63a5\uff09\uff0c\u8fd8\u662f\u4fdd\u7559\u4e00\u4e2a\u8868\u4e2d\u7684\u6240\u6709\u884c\uff0c\u4e0d\u8bba\u662f\u5426\u5339\u914d\u5230\uff08\u5de6\u6216\u53f3\u8fde\u63a5\uff09\uff0c\u6216\u8005\u662f\u4fdd\u7559\u4e24\u4e2a\u8868\u4e2d\u6240\u6709\u884c\uff08\u5168\u8fde\u63a5\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001MERGE() \u51fd\u6570\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><h4>\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>merge()<\/code> \u51fd\u6570\u662fR\u8bed\u8a00\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u5408\u5e76\u51fd\u6570\u4e4b\u4e00\u3002\u5b83\u53ef\u4ee5\u6839\u636e\u4e00\u4e2a\u6216\u591a\u4e2a\u5171\u540c\u7684\u5217\uff08\u5373\u952e\uff09\u6765\u5408\u5e76\u4e24\u4e2a\u8868\u3002\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">merge(x, y, by = &quot;key&quot;, by.x = NULL, by.y = NULL, all = FALSE, all.x = FALSE, all.y = FALSE)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d <code>x<\/code> \u548c <code>y<\/code> \u662f\u9700\u8981\u5408\u5e76\u7684\u4e24\u4e2a\u6570\u636e\u6846\uff0c<code>by<\/code> \u662f\u5171\u540c\u7684\u5217\uff0c<code>all<\/code> \u7c7b\u4f3c\u4e8eSQL\u4e2d\u7684\u5168\u8fde\u63a5\uff0c<code>all.x<\/code> \u548c <code>all.y<\/code> \u5206\u522b\u4ee3\u8868\u5de6\u8fde\u63a5\u548c\u53f3\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><h4>\u5b9e\u7528\u6280\u5de7<\/h4>\n<\/p>\n<p><p>\u5408\u7406\u5730\u5e94\u7528 <code>merge()<\/code> \u7684\u5404\u79cd\u53c2\u6570\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u8868\u5339\u914d\u9700\u6c42\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5185\u8fde\u63a5<\/strong>\uff1a\u5c06\u4e24\u4e2a\u8868\u4e2d\u5339\u914d\u7684\u884c\u5408\u5e76\u5728\u4e00\u8d77<\/li>\n<li><strong>\u5de6\u8fde\u63a5<\/strong>\uff1a\u4fdd\u7559\u5de6\u8fb9\u8868\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5b83\u4eec\u5728\u53f3\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u9879<\/li>\n<li><strong>\u53f3\u8fde\u63a5<\/strong>\uff1a\u4fdd\u7559\u53f3\u8fb9\u8868\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5b83\u4eec\u5728\u5de6\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u9879<\/li>\n<li><strong>\u5168\u8fde\u63a5<\/strong>\uff1a\u5408\u5e76\u4e24\u4e2a\u8868\u4e2d\u7684\u6240\u6709\u884c\uff0c\u4e0d\u7ba1\u5b83\u4eec\u662f\u5426\u6709\u5339\u914d\u9879<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001MATCH() \u51fd\u6570\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>\u5339\u914d\u5355\u5217\u6570\u636e<\/h4>\n<\/p>\n<p><p><code>match()<\/code> \u51fd\u6570\u5728R\u4e2d\u7528\u4e8e\u67e5\u627e\u4e00\u4e2a\u5411\u91cf\u4e2d\u7684\u5143\u7d20\u5728\u53e6\u4e00\u4e2a\u5411\u91cf\u4e2d\u7684\u4f4d\u7f6e\u3002\u5f53\u7528\u4e8e\u5339\u914d\u4e24\u4e2a\u8868\u7684\u6570\u636e\u65f6\uff0c<code>match()<\/code> \u53ef\u4ee5\u627e\u51fa\u4e00\u4e2a\u8868\u4e2d\u5217\u7684\u503c\u5728\u53e6\u4e00\u4e2a\u8868\u4e2d\u7684\u5bf9\u5e94\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">match(x, table, nomatch = NA_integer_, incomparables = NULL)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5339\u914d\u591a\u5217\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u867d\u7136 <code>match()<\/code> \u51fd\u6570\u901a\u5e38\u7528\u4e8e\u5355\u5217\u5339\u914d\uff0c\u4f46\u53ef\u4ee5\u6269\u5c55\u4f7f\u7528\uff0c\u7ed3\u5408\u5176\u4ed6R\u8bed\u8a00\u7279\u6027\u6765\u8fbe\u5230\u5339\u914d\u591a\u5217\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001DYPLR \u5305\u4e2d\u7684 JOIN \u51fd\u6570<\/h3>\n<\/p>\n<p><h4>INNER_JOIN() \u51fd\u6570<\/h4>\n<\/p>\n<p><p>dplyr\u5305\u63d0\u4f9b\u4e86\u4e00\u7ec4\u4e13\u4e3a\u6570\u636e\u64cd\u4f5c\u8bbe\u8ba1\u7684\u51fd\u6570\uff0c\u5176\u4e2d <code>inner_join()<\/code> \u662f\u975e\u5e38\u5b9e\u7528\u7684\u6570\u636e\u5339\u914d\u51fd\u6570\uff0c\u7528\u4e8e\u5339\u914d\u4e24\u4e2a\u8868\u4e2d\u5171\u6709\u7684\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">inner_join(x, y, by = NULL, copy = FALSE, suffix = c(&quot;.x&quot;, &quot;.y&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>LEFT_JOIN() \u4e0e\u5176\u4ed6\u8fde\u63a5\u51fd\u6570<\/h4>\n<\/p>\n<p><p>dplyr\u5305\u4e2d\u8fd8\u6709 <code>left_join()<\/code>\u3001<code>right_join()<\/code> \u548c <code>full_join()<\/code> \u7b49\u51fd\u6570\uff0c\u5b83\u4eec\u5206\u522b\u5b9e\u73b0\u4e86\u5de6\u8fde\u63a5\u3001\u53f3\u8fde\u63a5\u548c\u5168\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">left_join(x, y, by = NULL, copy = FALSE, suffix = c(&quot;.x&quot;, &quot;.y&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u8fd9\u4e9b\u51fd\u6570\u975e\u5e38\u65b9\u4fbf\u5730\u6a21\u62df\u4e86SQL\u4e2d\u7684\u8fde\u63a5\u64cd\u4f5c\uff0c\u7b80\u5316\u4e86R\u8bed\u8a00\u4e2d\u7684\u8868\u5339\u914d\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u9ad8\u7ea7\u5339\u914d\u6280\u5de7<\/h3>\n<\/p>\n<p><h4>\u591a\u952e\u5339\u914d<\/h4>\n<\/p>\n<p><p>\u5728\u73b0\u5b9e\u4e16\u754c\u7684\u6570\u636e\u5904\u7406\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u6839\u636e\u591a\u4e2a\u5b57\u6bb5\u8fdb\u884c\u5339\u914d\u3002\u5728\u6b64\u60c5\u5883\u4e0b\uff0c\u5408\u5e76\u51fd\u6570\u9700\u8981\u5bf9\u591a\u5217\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u6761\u4ef6\u5339\u914d<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u9700\u8981\u6839\u636e\u975e\u6807\u51c6\u7684\u5339\u914d\u903b\u8f91\u8fdb\u884c\u6570\u636e\u8868\u5408\u5e76\uff0c\u8fd9\u65f6\u5019\u53ef\u4ee5\u8fd0\u7528\u6761\u4ef6\u8bed\u53e5\u548c\u81ea\u5b9a\u4e49\u7684\u5339\u914d\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u6027\u80fd\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\u7684\u5339\u914d\uff0c\u6027\u80fd\u53ef\u80fd\u6210\u4e3a\u4e00\u4e2a\u95ee\u9898\u3002\u4f7f\u7528 <code>data.table<\/code> \u5305\u6216\u8fdb\u884c\u6570\u636e\u9884\u5904\u7406\u80fd\u591f\u63d0\u5347\u6570\u636e\u5408\u5e76\u8fc7\u7a0b\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6848\u4f8b\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5177\u4f53\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u6df1\u5165\u7406\u89e3\u4e0d\u540c\u51fd\u6570\u5982\u4f55\u5e94\u7528\u4e8e\u5b9e\u9645\u573a\u666f\u4e2d\uff0c\u5982\u4f55\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u8868\u5339\u914d\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u660e\u786e\u4e0d\u540c\u51fd\u6570\u7684\u5e94\u7528\u573a\u666f\u548c\u9650\u5236\uff0c\u4ee5\u53ca\u9002\u5f53\u5730\u4f7f\u7528\u6570\u636e\u9884\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u5f97\u5728R\u8bed\u8a00\u4e2d\u5339\u914d\u4e24\u4e2a\u8868\u7684\u6570\u636e\u66f4\u52a0\u6709\u6548\u548c\u51c6\u786e\u3002\u4e0d\u65ad\u5b9e\u8df5\uff0c\u4e86\u89e3\u6700\u65b0\u7684R\u5305\u548c\u51fd\u6570\uff0c\u4fdd\u6301\u7075\u6d3b\u7684\u5339\u914d\u7b56\u7565\uff0c\u662f\u59cb\u7ec8\u8d70\u5728\u6570\u636e\u5904\u7406\u524d\u6cbf\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1: R\u8bed\u8a00\u4e2d\u5982\u4f55\u8fdb\u884c\u8868\u6570\u636e\u7684\u5339\u914d\uff1f<\/strong><\/p>\n<p>\u5728R\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u8868\u6570\u636e\u7684\u5339\u914d\u3002\u5176\u4e2d\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>merge()<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u4e24\u4e2a\u8868\u4e2d\u7684\u4e00\u4e2a\u6216\u591a\u4e2a\u5171\u540c\u7684\u5217\u8fdb\u884c\u5339\u914d\uff0c\u5e76\u5c06\u5339\u914d\u540e\u7684\u7ed3\u679c\u5408\u5e76\u6210\u4e00\u4e2a\u65b0\u7684\u8868\u3002<\/p>\n<p><strong>Q2: \u5982\u4f55\u6839\u636e\u7279\u5b9a\u5217\u8fdb\u884c\u8868\u6570\u636e\u7684\u5339\u914d\uff1f<\/strong><\/p>\n<p>\u8981\u6839\u636e\u7279\u5b9a\u5217\u8fdb\u884c\u8868\u6570\u636e\u7684\u5339\u914d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u4e24\u4e2a\u8868\u4e2d\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u5171\u540c\u7684\u5217\u3002\u53ef\u4ee5\u4f7f\u7528<code>by<\/code>\u53c2\u6570\u6307\u5b9a\u8981\u5339\u914d\u7684\u5217\u540d\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8868<code>table1<\/code>\u548c<code>table2<\/code>\uff0c\u5b83\u4eec\u90fd\u6709\u4e00\u4e2a\u540d\u4e3a<code>ID<\/code>\u7684\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u5339\u914d\uff1a<\/p>\n<pre><code class=\"language-R\">merged_table &lt;- merge(table1, table2, by = &quot;ID&quot;)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u6839\u636e<code>ID<\/code>\u5217\u5c06\u4e24\u4e2a\u8868\u7684\u6570\u636e\u8fdb\u884c\u5339\u914d\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728<code>merged_table<\/code>\u4e2d\u3002<\/p>\n<p><strong>Q3: \u5982\u4f55\u5904\u7406\u5339\u914d\u8fc7\u7a0b\u4e2d\u7684\u7f3a\u5931\u503c\uff1f<\/strong><\/p>\n<p>\u5728\u8868\u6570\u636e\u5339\u914d\u8fc7\u7a0b\u4e2d\uff0c\u6709\u65f6\u4f1a\u9047\u5230\u67d0\u4e2a\u8868\u4e2d\u7684\u7279\u5b9a\u884c\u5728\u53e6\u4e00\u4e2a\u8868\u4e2d\u6ca1\u6709\u76f8\u5e94\u7684\u5339\u914d\u3002\u8fd9\u65f6\uff0c\u5339\u914d\u7ed3\u679c\u4e2d\u4f1a\u51fa\u73b0\u7f3a\u5931\u503c\uff08NA\uff09\u3002<\/p>\n<p>\u5728R\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>merge()<\/code>\u51fd\u6570\u7684<code>all.x<\/code>\u6216<code>all.y<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\u3002\u5982\u679c\u8bbe\u7f6e<code>all.x = TRUE<\/code>\uff0c\u5219\u4f1a\u4fdd\u7559\u5de6\u8fb9\u8868\uff08\u7b2c\u4e00\u4e2a\u8868\uff09\u4e2d\u7684\u6240\u6709\u884c\uff0c\u5e76\u5728\u6ca1\u6709\u5339\u914d\u7684\u60c5\u51b5\u4e0b\u7528\u7f3a\u5931\u503c\u586b\u5145\u3002\u5982\u679c\u8bbe\u7f6e<code>all.y = TRUE<\/code>\uff0c\u5219\u4f1a\u4fdd\u7559\u53f3\u8fb9\u8868\uff08\u7b2c\u4e8c\u4e2a\u8868\uff09\u4e2d\u7684\u6240\u6709\u884c\uff0c\u5e76\u5728\u6ca1\u6709\u5339\u914d\u7684\u60c5\u51b5\u4e0b\u7528\u7f3a\u5931\u503c\u586b\u5145\u3002\u4e5f\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>all = TRUE<\/code>\u6765\u4fdd\u7559\u6240\u6709\u884c\uff0c\u5305\u62ec\u6ca1\u6709\u5339\u914d\u7684\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6839\u636e<code>ID<\/code>\u5217\u5c06\u4e24\u4e2a\u8868\u7684\u6570\u636e\u8fdb\u884c\u5339\u914d\uff0c\u5e76\u4e14\u5728\u6ca1\u6709\u5339\u914d\u7684\u60c5\u51b5\u4e0b\u7528<code>NA<\/code>\u586b\u5145\u7f3a\u5931\u503c\uff1a<\/p>\n<pre><code class=\"language-R\">merged_table &lt;- merge(table1, table2, by = &quot;ID&quot;, all = TRUE)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u7528R\u8bed\u8a00\u5339\u914d\u4e24\u4e2a\u8868\u7684\u6570\u636e \u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684\u51fd\u6570\u5b9e\u73b0\uff0c\u5982 merge()\u3001match()\u3001inner_join() [&hellip;]","protected":false},"author":3,"featured_media":180210,"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\/180205"}],"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=180205"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180205\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/180210"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=180205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=180205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=180205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}