{"id":180340,"date":"2024-05-08T20:33:20","date_gmt":"2024-05-08T12:33:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/180340.html"},"modified":"2024-05-08T20:33:27","modified_gmt":"2024-05-08T12:33:27","slug":"%e5%a6%82%e4%bd%95%e5%af%b9r%e8%af%ad%e8%a8%80%e6%95%b0%e6%8d%ae%e6%a1%86%e4%b8%ad%e7%9a%84%e6%95%b0%e6%8d%ae%e6%8c%89%e8%a1%8c%e9%87%8d%e6%8e%92","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/180340.html","title":{"rendered":"\u5982\u4f55\u5bf9R\u8bed\u8a00\u6570\u636e\u6846\u4e2d\u7684\u6570\u636e\u6309\u884c\u91cd\u6392"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27065511\/b80fd80e-d9cb-4f3b-bf45-152276dc88a4.webp\" alt=\"\u5982\u4f55\u5bf9R\u8bed\u8a00\u6570\u636e\u6846\u4e2d\u7684\u6570\u636e\u6309\u884c\u91cd\u6392\" \/><\/p>\n<p><p>\u6570\u636e\u91cd\u6392\u5bf9\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u6765\u8bf4\u81f3\u5173\u91cd\u8981\uff0c<strong>R\u8bed\u8a00\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd<\/strong>\uff0c\u5176\u4e2d\u5305\u62ec\u5bf9\u6570\u636e\u6846(Data Frame)\u4e2d\u7684\u6570\u636e\u6309\u884c\u91cd\u6392\u3002\u5bf9\u6570\u636e\u6846\u6309\u884c\u91cd\u6392\u901a\u5e38\u9700\u8981\u57fa\u4e8e\u4e00\u5b9a\u7684\u6392\u5e8f\u89c4\u5219\u6216\u8005\u968f\u673a\u7684\u5e8f\u5217\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>order()<\/code>\u51fd\u6570\u4ee5\u6307\u5b9a\u5217\u7684\u503c\u6392\u5e8f\uff0c\u4f7f\u7528<code>sample()<\/code>\u51fd\u6570\u968f\u673a\u91cd\u6392\u884c\uff0c\u6216\u8005\u5229\u7528<code>dplyr<\/code>\u5305\u4e2d\u7684<code>arrange()<\/code>\u51fd\u6570\u6765\u5b9a\u5236\u884c\u6392\u5e8f\u89c4\u5219\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4f1a\u6df1\u5165\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u4e3a\u60a8\u63d0\u4f9b\u5728\u8fdb\u884c\u6570\u636e\u5206\u6790\u65f6\u5982\u4f55\u6709\u6548\u5730\u5bf9\u6570\u636e\u6846\u6309\u884c\u8fdb\u884c\u91cd\u6392\u7684\u5177\u4f53\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e00\u3001USING THE <code>ORDER()<\/code> FUNCTION<\/strong><\/h3>\n<\/p>\n<p><p><code>order()<\/code>\u51fd\u6570\u662fR\u8bed\u8a00\u4e2d\u7528\u4e8e\u6392\u5e8f\u7684\u57fa\u7840\u51fd\u6570\u3002\u901a\u8fc7\u5bf9\u7279\u5b9a\u5217\u8fdb\u884c\u6392\u5e8f\u6765\u6539\u53d8\u884c\u7684\u4f4d\u7f6e\u901a\u5e38\u662f\u6570\u636e\u5206\u6790\u7684\u4e00\u4e2a\u521d\u59cb\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>order()<\/code>\u7684\u57fa\u672c\u8bed\u6cd5\u662f\u6307\u5b9a\u4f60\u60f3\u8981\u6392\u5e8f\u7684\u5217\u540d<\/strong>\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8981\u6839\u636e\u6570\u636e\u6846\u4e2d\u7684\u67d0\u5217\u201cx\u201d\u5bf9\u6574\u4e2a\u6570\u636e\u6846\u8fdb\u884c\u6392\u5e8f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801:<\/p>\n<\/p>\n<p><pre><code class=\"language-r\"># \u5047\u8bbedf\u662f\u6211\u4eec\u7684\u6570\u636e\u6846<\/p>\n<p>df_ordered &lt;- df[order(df$x), ]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u60c5\u51b5\u4e0b\uff0c<code>order(df$x)<\/code>\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6392\u5e8f\u4f4d\u7f6e\u7d22\u5f15\u7684\u5411\u91cf\uff0c\u7136\u540e\u6211\u4eec\u4f7f\u7528\u8fd9\u4e2a\u5411\u91cf\u6765\u5bf9\u6570\u636e\u6846<code>df<\/code>\u91cd\u65b0\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63a2\u8ba8<code>order()<\/code><\/h4>\n<\/p>\n<p><p><code>order()<\/code>\u51fd\u6570\u7684\u5f3a\u5927\u4e4b\u5904\u5728\u4e8e\u5b83\u80fd\u591f\u8fdb\u884c\u591a\u5217\u7684\u6392\u5e8f\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u5347\u5e8f\u6216\u964d\u5e8f\u3002\u5982\u679c\u8981\u6839\u636e\u591a\u4e2a\u5217\u8fdb\u884c\u6392\u5e8f\uff0c\u53ea\u9700\u5728\u51fd\u6570\u4e2d\u6dfb\u52a0\u5217\u540d\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">df_ordered &lt;- df[order(df$x, df$y), ]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u4ee3\u7801\u4e2d\uff0c\u6570\u636e\u6846<code>df<\/code>\u9996\u5148\u6309\u7167\u201cx\u201d\u5217\u6392\u5e8f\uff0c\u7136\u540e\u5728\u201cx\u201d\u5217\u7684\u503c\u76f8\u540c\u7684\u60c5\u51b5\u4e0b\uff0c\u6309\u7167\u201cy\u201d\u5217\u7684\u503c\u8fdb\u884c\u6b21\u7ea7\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e8c\u3001APPLYING THE <code>SAMPLE()<\/code> FUNCTION<\/strong><\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5c06\u6570\u636e\u968f\u673a\u5316\uff0c\u4f8b\u5982\uff0c\u5728\u8fdb\u884c\u62bd\u6837\u6216\u9a8c\u8bc1\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u65f6\u3002\u5728R\u4e2d\uff0c<strong>\u4f7f\u7528<code>sample()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5730\u968f\u673a\u91cd\u6392\u6570\u636e\u6846\u4e2d\u7684\u884c<\/strong>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">set.seed(123) # \u8bbe\u7f6e\u79cd\u5b50\u4fdd\u8bc1\u53ef\u91cd\u590d\u6027<\/p>\n<p>df_shuffled &lt;- df[sample(nrow(df)), ]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>sample(nrow(df))<\/code>\u751f\u6210\u4ece1\u5230\u6570\u636e\u6846\u884c\u6570\u7684\u91cd\u6392\u7684\u968f\u673a\u6574\u6570\u5411\u91cf\u3002\u7136\u540e\u6211\u4eec\u4f7f\u7528\u8fd9\u4e2a\u5411\u91cf\u6765\u5bf9\u6570\u636e\u6846\u7684\u884c\u8fdb\u884c\u91cd\u6392\u3002<\/p>\n<\/p>\n<p><h4>\u63a2\u7d22<code>sample()<\/code>\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p><code>sample()<\/code>\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u751f\u6210\u968f\u673a\u91cd\u6392\u7684\u7d22\u5f15\uff0c\u8fd8\u53ef\u4ee5\u6307\u5b9a\u62bd\u6837\u7684\u5927\u5c0f\u3002\u5982\u679c\u4f60\u4e0d\u9700\u8981\u91cd\u6392\u6574\u4e2a\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>size<\/code>\u53c2\u6570\u6307\u5b9a\u6240\u9700\u7684\u884c\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">df_sampled &lt;- df[sample(nrow(df), size = 10), ]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u53ea\u968f\u673a\u62bd\u53d6\u4e8610\u884c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e09\u3001UTILIZING <code>DPLYR<\/code> FOR ROW ARRANGEMENT<\/strong><\/h3>\n<\/p>\n<p><p><code>dplyr<\/code>\u662f\u4e00\u4e2a\u5e7f\u53d7\u6b22\u8fce\u7684R\u5305\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u3002\u5176\u4e2d\u7684<code>arrange()<\/code>\u51fd\u6570\u662f\u5bf9\u6570\u636e\u6846\u6309\u884c\u8fdb\u884c\u6392\u5e8f\u7684\u7406\u60f3\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>arrange()<\/code>\u53ef\u4ee5\u7075\u6d3b\u5730\u6839\u636e\u4e00\u5217\u6216\u591a\u5217\u8fdb\u884c\u6392\u5e8f<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">library(dplyr)<\/p>\n<p>df_arranged &lt;- df %&gt;% arrange(x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6df1\u5165\u4e86\u89e3<code>dplyr<\/code>\u7684<code>arrange()<\/code><\/h4>\n<\/p>\n<p><p>\u5728<code>dplyr<\/code>\u4e2d\uff0c<code>arrange()<\/code>\u53ef\u4ee5\u914d\u5408\u4f7f\u7528\u5176\u4ed6\u51fd\u6570\u5982<code>desc()<\/code>\u6765\u5bf9\u67d0\u5217\u6570\u636e\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\u3002\u5982\u679c\u8981\u9488\u5bf9\u591a\u4e2a\u5217\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">df_arranged &lt;- df %&gt;% arrange(x, desc(y))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6570\u636e\u5148\u6839\u636e\u201cx\u201d\u5217\u7684\u5347\u5e8f\u6392\u5217\uff0c\u7136\u540e\u5728\u201cx\u201d\u76f8\u540c\u7684\u60c5\u51b5\u4e0b\u6309\u201cy\u201d\u5217\u7684\u964d\u5e8f\u6392\u5217\u3002<\/p>\n<\/p>\n<p><h3><strong>\u56db\u3001COMBINING SORTING AND FILTERING<\/strong><\/h3>\n<\/p>\n<p><p>\u7ecf\u5e38\uff0c\u6392\u5e8f\u9700\u8981\u4e0e\u8fc7\u6ee4\u6570\u636e\u884c\u76f8\u7ed3\u5408\uff0c\u4ee5\u7b5b\u9009\u51fa\u7279\u5b9a\u7684\u6570\u636e\u5b50\u96c6\u8fdb\u884c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">df_filtered_sorted &lt;- df %&gt;%<\/p>\n<p>  filter(z &gt; 0) %&gt;%<\/p>\n<p>  arrange(desc(z))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>filter(z &gt; 0)<\/code>\u9996\u5148\u7b5b\u9009\u51fa\u201cz\u201d\u5217\u503c\u5927\u4e8e0\u7684\u884c\uff0c\u7136\u540e<code>arrange(desc(z))<\/code>\u5bf9\u8fd9\u4e9b\u884c\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h4><code>dplyr<\/code>\u94fe\u5f0f\u7ec4\u5408<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>dplyr<\/code>\u7684\u94fe\u5f0f\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u6570\u636e\u5904\u7406\u66f4\u52a0\u76f4\u89c2\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0d\u65ad\u6dfb\u52a0\u66f4\u591a\u7684\u6b65\u9aa4\u6765\u7cbe\u7ec6\u5316\u5bf9\u6570\u636e\u7684\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">df_complex &lt;- df %&gt;%<\/p>\n<p>  filter(z &gt;= 0 &amp; y == &quot;specific_value&quot;) %&gt;%<\/p>\n<p>  arrange(x, desc(y)) %&gt;%<\/p>\n<p>  select(x, y, z)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u590d\u6742\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8fc7\u6ee4\u6570\u636e\uff0c\u7136\u540e\u6392\u5e8f\uff0c\u5e76\u6700\u7ec8\u9009\u62e9\u7279\u5b9a\u5217\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e94\u3001ADVANCED SORTING WITH TIES<\/strong><\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6392\u5e8f\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e0d\u540c\u884c\u7684\u6392\u5e8f\u5217\u5177\u6709\u76f8\u540c\u503c\u7684\u60c5\u51b5\uff08\u5373\u5e73\u5c40\u6216Ties\uff09\u3002<code>order()<\/code>\u51fd\u6570\u5982\u4f55\u5904\u7406\u5e73\u5c40\u5bf9\u7ed3\u679c\u6709\u91cd\u8981\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><p>\u5f53\u4f7f\u7528<code>order()<\/code>\u51fd\u6570\u8fdb\u884c\u6392\u5e8f\u65f6\uff0c\u82e5\u4e24\u884c\u7684\u6392\u5e8f\u4f9d\u636e\u5217\u76f8\u540c\uff0c\u5b83\u4eec\u5728\u7ed3\u679c\u4e2d\u7684\u987a\u5e8f\u5c06\u4fdd\u6301\u539f\u59cb\u6570\u636e\u4e2d\u7684\u987a\u5e8f\u3002\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5f15\u5165\u989d\u5916\u7684\u5217\u6765\u6253\u7834\u5e73\u5c40\uff0c\u6216\u8005\u662f\u4f7f\u7528<code>sample()<\/code>\u51fd\u6570\u4e3a\u5177\u6709\u76f8\u540c\u503c\u7684\u8fd9\u4e9b\u884c\u968f\u673a\u8d4b\u4e88\u4e00\u4e2a\u65b0\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e3a\u4e86\u66f4\u7cbe\u786e\u5730\u5904\u7406\u5e73\u5c40<\/strong>\uff0c\u4f60\u53ef\u4ee5\u6309\u5982\u4e0b\u7684\u4ee3\u7801\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">df_tied &lt;- df[order(df$x, df$y, sample(nrow(df))), ]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>sample(nrow(df))<\/code>\u786e\u4fdd\u4e86\u5728\u76f8\u540c\u201cx\u201d\u548c\u201cy\u201d\u503c\u7684\u60c5\u51b5\u4e0b\u5bf9\u884c\u8fdb\u884c\u968f\u673a\u6392\u5e8f\uff0c\u4ee5\u6b64\u6765\u6253\u7834\u5e73\u5c40\u3002<\/p>\n<\/p>\n<p><h4><strong>\u7efc\u5408\u8ba8\u8bba\u5e73\u5c40\u7684\u5904\u7406<\/strong><\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7684\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u5e73\u5c40\u7684\u5904\u7406\u4e0d\u4ec5\u9700\u8981\u8003\u8651\u6392\u5e8f\u7684\u516c\u5e73\u6027\uff0c\u4e5f\u9700\u8981\u8003\u8651\u5230\u6392\u5e8f\u7684\u7a33\u5b9a\u6027\uff0c\u5c24\u5176\u662f\u5f53\u7ed3\u679c\u4f1a\u88ab\u7528\u4e8e\u8fdb\u4e00\u6b65\u5206\u6790\u65f6\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u4e0d\u540c\u7684\u5217\u91c7\u53d6\u4e0d\u540c\u7684\u7b56\u7565\u6765\u5904\u7406\u5e73\u5c40\u3002<\/p>\n<\/p>\n<p><h3><strong>\u516d\u3001CONCLUSION AND BEST PRACTICES<\/strong><\/h3>\n<\/p>\n<p><p>\u5c06\u8fd9\u4e9b\u6280\u80fd\u5e94\u7528\u4e8e\u5b9e\u9645\u60c5\u51b5\u65f6\uff0c\u6709\u51e0\u4e2a\u6700\u4f73\u5b9e\u8df5\u9700\u8981\u9075\u5faa\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728\u8fdb\u884c\u4efb\u4f55\u5f62\u5f0f\u7684\u6570\u636e\u91cd\u6392\u524d\uff0c<strong>\u59cb\u7ec8\u786e\u4fdd\u4f60\u6709\u6570\u636e\u7684\u5907\u4efd<\/strong>\u3002<\/li>\n<li>\u4f7f\u7528<code>set.seed()<\/code>\u4fdd\u8bc1\u968f\u673a\u6392\u5e8f\u7684\u53ef\u590d\u73b0\u6027\uff0c\u8fd9\u5bf9\u79d1\u5b66\u7814\u7a76\u5c24\u5176\u91cd\u8981\u3002<\/li>\n<li>\u5f53\u4f7f\u7528<code>dplyr<\/code>\u8fdb\u884c\u6392\u5e8f\u6216\u5176\u4ed6\u6570\u636e\u64cd\u4f5c\u65f6\uff0c\u786e\u4fdd\u7406\u89e3<code>%&gt;%<\/code>\u7ba1\u9053\u64cd\u4f5c\u7b26\u7684\u903b\u8f91\u4ee5\u53ca\u5b83\u662f\u5982\u4f55\u5f71\u54cd\u6570\u636e\u5904\u7406\u6d41\u7a0b\u7684\u3002<\/li>\n<\/ul>\n<p><p>\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u4ee5\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5f00\u5c55R\u8bed\u8a00\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u6570\u636e\u91cd\u6392\u5de5\u4f5c\u3002\u65e0\u8bba\u662f\u6309\u7279\u5b9a\u89c4\u5219\u8fd8\u662f\u968f\u673a\u91cd\u6392\uff0cR\u8bed\u8a00\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u5e2e\u52a9\u4f60\u5b9e\u73b0\u76ee\u6807\uff0c\u4f18\u5316\u4f60\u7684\u6570\u636e\u5206\u6790\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528R\u8bed\u8a00\u5bf9\u6570\u636e\u6846\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u6309\u884c\u91cd\u6392\uff1f<\/strong><\/p>\n<p>\u6570\u636e\u6846\u4e2d\u7684\u6570\u636e\u6309\u884c\u91cd\u6392\u662f\u5f88\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528R\u8bed\u8a00\u4e2d\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4e00\u4e2a\u5e38\u7528\u7684\u51fd\u6570\u662f<code>order()<\/code>\uff0c\u5b83\u53ef\u4ee5\u5bf9\u6570\u636e\u6846\u4e2d\u7684\u5217\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de\u6392\u5e8f\u540e\u7684\u884c\u7d22\u5f15\u3002<\/p>\n<pre><code class=\"language-R\"># \u5047\u8bbedf\u662f\u4f60\u7684\u6570\u636e\u6846\ndf_reordered &lt;- df[order(row.names(df)), ]\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5c06\u4f1a\u6309\u7167\u884c\u7d22\u5f15\u7684\u5347\u5e8f\u5bf9\u6570\u636e\u6846\u8fdb\u884c\u91cd\u6392\u3002\u4f60\u4e5f\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u4fee\u6539\u4ee3\u7801\uff0c\u4f8b\u5982\u6309\u884c\u7d22\u5f15\u7684\u964d\u5e8f\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u57fa\u4e8etidyverse\u5305\u7684<code>arrange()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u91cd\u6392\uff0c\u8fd9\u4e2a\u51fd\u6570\u66f4\u52a0\u76f4\u89c2\u6613\u61c2\u3002<\/p>\n<pre><code class=\"language-R\">library(tidyverse)\n\n# \u5047\u8bbedf\u662f\u4f60\u7684\u6570\u636e\u6846\ndf_reordered &lt;- df %&gt;%\n  arrange(row.names(df))\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5c06\u4f1a\u6309\u7167\u884c\u7d22\u5f15\u7684\u5347\u5e8f\u5bf9\u6570\u636e\u6846\u8fdb\u884c\u91cd\u6392\uff0c\u4f60\u4e5f\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u4fee\u6539\u4ee3\u7801\u3002<\/p>\n<p><strong>2. \u5728R\u4e2d\u5982\u4f55\u5bf9\u6570\u636e\u6846\u4e2d\u7684\u6570\u636e\u6309\u884c\u91cd\u65b0\u6392\u5e8f\uff1f<\/strong><\/p>\n<p>\u8981\u6309\u884c\u5bf9\u6570\u636e\u6846\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u91cd\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528R\u8bed\u8a00\u7684<code>dplyr<\/code>\u5305\u4e2d\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c<code>arrange()<\/code>\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u5217\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<pre><code class=\"language-R\">library(dplyr)\n\n# \u5047\u8bbedf\u662f\u4f60\u7684\u6570\u636e\u6846\ndf_reordered &lt;- df %&gt;%\n  arrange(row_number())\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5c06\u4f1a\u6309\u7167\u884c\u53f7\u7684\u5347\u5e8f\u5bf9\u6570\u636e\u6846\u8fdb\u884c\u91cd\u6392\u3002\u4f60\u4e5f\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u4fee\u6539\u4ee3\u7801\uff0c\u4f8b\u5982\u6309\u7167\u5176\u4ed6\u5217\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u57fa\u4e8ebase R\u7684<code>order()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u91cd\u6392\u3002<\/p>\n<pre><code class=\"language-R\"># \u5047\u8bbedf\u662f\u4f60\u7684\u6570\u636e\u6846\ndf_reordered &lt;- df[order(row_number()), ]\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5c06\u4f1a\u6309\u7167\u884c\u53f7\u7684\u5347\u5e8f\u5bf9\u6570\u636e\u6846\u8fdb\u884c\u91cd\u6392\uff0c\u4f60\u4e5f\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u4fee\u6539\u4ee3\u7801\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5229\u7528R\u8bed\u8a00\u5bf9\u6570\u636e\u6846\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u6309\u884c\u6392\u5e8f\uff1f<\/strong><\/p>\n<p>\u8981\u5bf9R\u8bed\u8a00\u4e2d\u7684\u6570\u636e\u6846\u6309\u884c\u8fdb\u884c\u6392\u5e8f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>order()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u5217\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de\u6392\u5e8f\u540e\u7684\u884c\u7d22\u5f15\u3002<\/p>\n<pre><code class=\"language-R\"># \u5047\u8bbedf\u662f\u4f60\u7684\u6570\u636e\u6846\ndf_reordered &lt;- df[order(row.names(df)), ]\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u4f1a\u6309\u7167\u884c\u7d22\u5f15\u7684\u5347\u5e8f\u5bf9\u6570\u636e\u6846\u8fdb\u884c\u6392\u5e8f\u3002\u4f60\u4e5f\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u4fee\u6539\u4ee3\u7801\uff0c\u4f8b\u5982\u6309\u7167\u5176\u4ed6\u5217\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>arrange()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u6392\u5e8f\uff0c\u8fd9\u4e2a\u51fd\u6570\u662f\u57fa\u4e8etidyverse\u5305\u7684\u3002<\/p>\n<pre><code class=\"language-R\">library(tidyverse)\n\n# \u5047\u8bbedf\u662f\u4f60\u7684\u6570\u636e\u6846\ndf_reordered &lt;- df %&gt;%\n  arrange(row.names(df))\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5c06\u4f1a\u6309\u7167\u884c\u7d22\u5f15\u7684\u5347\u5e8f\u5bf9\u6570\u636e\u6846\u8fdb\u884c\u6392\u5e8f\uff0c\u4f60\u4e5f\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u4fee\u6539\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6570\u636e\u91cd\u6392\u5bf9\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u6765\u8bf4\u81f3\u5173\u91cd\u8981\uff0cR\u8bed\u8a00\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u5176\u4e2d\u5305\u62ec\u5bf9\u6570\u636e\u6846(Data Frame [&hellip;]","protected":false},"author":3,"featured_media":180344,"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\/180340"}],"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=180340"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180340\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/180344"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=180340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=180340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=180340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}