{"id":5854,"date":"2025-07-03T15:01:54","date_gmt":"2025-07-03T07:01:54","guid":{"rendered":"http:\/\/192.168.1.29\/?p=5854"},"modified":"2025-07-03T15:01:54","modified_gmt":"2025-07-03T07:01:54","slug":"r-%e4%bb%a3%e7%a0%81%e7%94%a8%e4%ba%8e%e5%af%b9mtcars%e6%95%b0%e6%8d%ae%e9%9b%86%e8%bf%9b%e8%a1%8c%e5%88%86%e7%bb%84%e6%b1%87%e6%80%bb%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"http:\/\/cnliutz.wicp.vip\/?p=5854","title":{"rendered":"\u00a0R \u4ee3\u7801\u7528\u4e8e\u5bf9mtcars\u6570\u636e\u96c6\u8fdb\u884c\u5206\u7ec4\u6c47\u603b\u5206\u6790"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>library(magrittr)\nlibrary(dplyr)\nmtcars\nmtcars %>%\n  group_by(cyl, gear) %>% \n  summarise_all(list(mean), na.rm=TRUE)\n\n\n\u8fd9\u6bb5R\u4ee3\u7801\u4e3b\u8981\u7528\u4e8e\u5bf9<code>mtcars<\/code>\u6570\u636e\u96c6\u8fdb\u884c\u5206\u7ec4\u6c47\u603b\u5206\u6790\uff0c\u5177\u4f53\u529f\u80fd\u548c\u610f\u4e49\u5982\u4e0b\uff1a<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u5305\u7684\u52a0\u8f7d<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>library(magrittr)<\/code><\/strong>\uff1a\u52a0\u8f7d<code>magrittr<\/code>\u5305\uff0c\u7528\u4e8e\u4f7f\u7528\u7ba1\u9053\u8fd0\u7b97\u7b26<code>%>%<\/code>\u3002\u8be5\u8fd0\u7b97\u7b26\u53ef\u5c06\u524d\u4e00\u6b65\u7684\u7ed3\u679c\u201c\u4f20\u9012\u201d\u7ed9\u540e\u4e00\u6b65\u51fd\u6570\uff0c\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\uff08\u4f8b\u5982<code>A %>% f() %>% g()<\/code>\u7b49\u4ef7\u4e8e<code>g(f(A))<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong><code>library(dplyr)<\/code><\/strong>\uff1a\u52a0\u8f7d<code>dplyr<\/code>\u5305\uff0c\u7528\u4e8e\u6570\u636e\u5904\u7406\u64cd\u4f5c\uff08\u5982\u5206\u7ec4\u3001\u6c47\u603b\u7b49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u6570\u636e\u67e5\u770b\u4e0e\u5904\u7406<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>mtcars<\/code><\/strong>\uff1a\u76f4\u63a5\u8f93\u51fa<code>mtcars<\/code>\u6570\u636e\u96c6\uff08R\u8bed\u8a00\u5185\u7f6e\u7684\u6c7d\u8f66\u6027\u80fd\u6570\u636e\u96c6\uff0c\u5305\u542b32\u884c11\u5217\uff0c\u8bb0\u5f55\u4e86\u4e0d\u540c\u8f66\u578b\u7684\u6cb9\u8017\u3001\u6c14\u7f38\u6570\u3001\u9a6c\u529b\u7b49\u53c2\u6570\uff09\u3002<\/li>\n\n\n\n<li><strong>\u7ba1\u9053\u64cd\u4f5c\u4e0e\u5206\u7ec4\u6c47\u603b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  mtcars %&gt;%\n    group_by(cyl, gear) %&gt;% \n    summarise_all(list(mean), na.rm=TRUE)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>group_by(cyl, gear)<\/code><\/strong>\uff1a\u6309<code>cyl<\/code>\uff08\u6c14\u7f38\u6570\uff09\u548c<code>gear<\/code>\uff08\u6321\u4f4d\u6570\u91cf\uff09\u4e24\u4e2a\u53d8\u91cf\u5bf9\u6570\u636e\u5206\u7ec4\u3002\u4f8b\u5982\uff0c\u5c06\u6240\u67094\u7f38\u30013\u6321\u7684\u8f66\u578b\u5f52\u4e3a\u4e00\u7ec4\uff0c6\u7f38\u30014\u6321\u7684\u5f52\u4e3a\u53e6\u4e00\u7ec4\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/li>\n\n\n\n<li><strong><code>summarise_all(list(mean), na.rm=TRUE)<\/code><\/strong>\uff1a\u5bf9\u6bcf\u4e2a\u5206\u7ec4\u5185\u7684\u6240\u6709\u6570\u503c\u578b\u53d8\u91cf\uff08\u5982<code>mpg<\/code>\u6cb9\u8017\u3001<code>disp<\/code>\u6392\u91cf\u7b49\uff09\u8ba1\u7b97\u5747\u503c\uff0c\u5e76\u5ffd\u7565\u7f3a\u5931\u503c\uff08<code>na.rm=TRUE<\/code>\uff09\u3002<code>summarise_all<\/code>\u4f1a\u5bf9\u6240\u6709\u5217\u6267\u884c\u6307\u5b9a\u51fd\u6570\uff0c<code>list(mean)<\/code>\u8868\u793a\u4f7f\u7528<code>mean<\/code>\u51fd\u6570\u8fdb\u884c\u6c47\u603b\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u4ee3\u7801\u7684\u6838\u5fc3\u610f\u4e49<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u5206\u6790\u76ee\u6807<\/strong>\uff1a\u901a\u8fc7\u5206\u7ec4\u6c47\u603b\uff0c\u5206\u6790\u4e0d\u540c\u6c14\u7f38\u6570\uff08<code>cyl<\/code>\uff09\u548c\u6321\u4f4d\u6570\u91cf\uff08<code>gear<\/code>\uff09\u7ec4\u5408\u4e0b\uff0c\u6c7d\u8f66\u5404\u9879\u6027\u80fd\u6307\u6807\u7684\u5e73\u5747\u6c34\u5e73\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6bd4\u8f834\u7f383\u6321\u8f66\u578b\u4e0e6\u7f385\u6321\u8f66\u578b\u7684\u5e73\u5747\u6cb9\u8017\uff08<code>mpg<\/code>\uff09\u3001\u5e73\u5747\u6392\u91cf\uff08<code>disp<\/code>\uff09\u7b49\u5dee\u5f02\u3002<\/li>\n\n\n\n<li><strong>\u4ee3\u7801\u4f18\u52bf<\/strong>\uff1a\u4f7f\u7528<code>dplyr<\/code>\u548c\u7ba1\u9053\u8fd0\u7b97\u7b26\u7684\u7ec4\u5408\uff0c\u4f7f\u6570\u636e\u5904\u7406\u6d41\u7a0b\u66f4\u6e05\u6670\uff08\u5206\u7ec4\u2192\u6c47\u603b\uff09\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u5f3a\uff0c\u7b26\u5408\u201c\u6570\u636e\u6d41\u5411\u201d\u7684\u903b\u8f91\uff08\u4ece\u539f\u59cb\u6570\u636e\u5230\u5206\u7ec4\u7ed3\u679c\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u9884\u671f\u8f93\u51fa\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<p>\u5047\u8bbe\u5206\u7ec4\u540e\u6709\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>cyl<\/th><th>gear<\/th><th>mpg_mean<\/th><th>disp_mean<\/th><th>hp_mean<\/th><th>\u2026<\/th><\/tr><\/thead><tbody><tr><td>4<\/td><td>3<\/td><td>22.5<\/td><td>108.0<\/td><td>93.0<\/td><td>\u2026<\/td><\/tr><tr><td>4<\/td><td>4<\/td><td>26.0<\/td><td>120.5<\/td><td>110.0<\/td><td>\u2026<\/td><\/tr><tr><td>6<\/td><td>4<\/td><td>19.2<\/td><td>167.6<\/td><td>123.0<\/td><td>\u2026<\/td><\/tr><tr><td>8<\/td><td>3<\/td><td>15.8<\/td><td>350.0<\/td><td>200.0<\/td><td>\u2026<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8be5\u7ed3\u679c\u53ef\u5e2e\u52a9\u5206\u6790\u4e0d\u540c\u8f66\u578b\u914d\u7f6e\u7684\u6027\u80fd\u7279\u5f81\uff0c\u4e3a\u540e\u7eed\u7edf\u8ba1\u5206\u6790\u6216\u53ef\u89c6\u5316\u63d0\u4f9b\u57fa\u7840\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u5305\u7684\u52a0\u8f7d 2. \u6570\u636e\u67e5\u770b\u4e0e\u5904\u7406 3. \u4ee3\u7801\u7684\u6838\u5fc3\u610f\u4e49 4. \u9884\u671f\u8f93\u51fa\u793a\u4f8b  <span class=\"readmore\"><a href=\"http:\/\/cnliutz.wicp.vip\/?p=5854\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-5854","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5854"}],"version-history":[{"count":1,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5854\/revisions"}],"predecessor-version":[{"id":5855,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5854\/revisions\/5855"}],"wp:attachment":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5854"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}