{"id":388,"date":"2023-03-17T15:02:31","date_gmt":"2023-03-17T15:02:31","guid":{"rendered":"https:\/\/yunshangtulv.com.cn\/?p=388"},"modified":"2023-03-17T15:11:56","modified_gmt":"2023-03-17T15:11:56","slug":"%e5%9b%a0%e5%ad%90%e8%af%a6%e8%a7%a3%e5%8f%8a%e5%ba%94%e7%94%a8%e4%b8%be%e4%be%8b","status":"publish","type":"post","link":"https:\/\/yunshangtulv.com.cn\/?p=388","title":{"rendered":"\u56e0\u5b50\u8be6\u89e3\u53ca\u5e94\u7528\u4e3e\u4f8b"},"content":{"rendered":"<p>R\u8bed\u8a00\u4e2d\u7684\u56e0\u5b50\u662f\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8e\u8868\u793a\u5206\u7c7b\u53d8\u91cf\u3002\u5728\u7edf\u8ba1\u5206\u6790\u4e2d\uff0c\u5206\u7c7b\u53d8\u91cf\u901a\u5e38\u662f\u6307\u79bb\u6563\u7684\uff0c\u6709\u9650\u7684\u53d6\u503c\u96c6\u5408\uff0c\u4f8b\u5982\u6027\u522b\uff08\u7537\/\u5973\uff09\u3001\u6559\u80b2\u7a0b\u5ea6\uff08\u9ad8\u4e2d\/\u672c\u79d1\/\u7855\u58eb\/\u535a\u58eb\uff09\u7b49\u3002\u5728R\u8bed\u8a00\u4e2d\uff0c\u56e0\u5b50\u901a\u8fc7\u5c06\u79bb\u6563\u7684\u53d6\u503c\u8f6c\u6362\u4e3a\u6570\u5b57\u7f16\u7801\u6765\u8868\u793a\u8fd9\u4e9b\u5206\u7c7b\u53d8\u91cf\u3002<\/p>\n<p>\u4ee5\u4e0b\u662fR\u8bed\u8a00\u4e2d\u56e0\u5b50\u7684\u8be6\u89e3\u53ca\u5e94\u7528\u5b9e\u4f8b\uff1a<\/p>\n<h2>\u521b\u5efa\u56e0\u5b50<\/h2>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>factor()<\/code>\u51fd\u6570\u521b\u5efa\u56e0\u5b50\u3002\u4f8b\u5982\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-bash\">gender &lt;- <span class=\"hljs-built_in\">factor<\/span>(c(<span class=\"hljs-string\">\"male\"<\/span>, <span class=\"hljs-string\">\"female\"<\/span>, <span class=\"hljs-string\">\"male\"<\/span>, <span class=\"hljs-string\">\"male\"<\/span>, <span class=\"hljs-string\">\"female\"<\/span>))<br \/>\n<\/code><\/div>\n<\/div>\n<p>\u8fd9\u91cc\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b5\u4e2a\u5143\u7d20\u7684\u56e0\u5b50<code>gender<\/code>\uff0c\u5176\u4e2d<code>\"male\"<\/code>\u548c<code>\"female\"<\/code>\u662f\u56e0\u5b50\u7684\u6c34\u5e73\uff08level\uff09\u3002<\/p>\n<h2>\u67e5\u770b\u56e0\u5b50\u4fe1\u606f<\/h2>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u67e5\u770b\u56e0\u5b50\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-scss\"><span class=\"hljs-built_in\">str<\/span>(gender)<br \/>\n<\/code><\/div>\n<\/div>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-json\">Factor w\/ <span class=\"hljs-number\">2<\/span> levels <span class=\"hljs-string\">\"female\"<\/span><span class=\"hljs-punctuation\">,<\/span><span class=\"hljs-attr\">\"male\"<\/span><span class=\"hljs-punctuation\">:<\/span> <span class=\"hljs-number\">2<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-number\">2<\/span> <span class=\"hljs-number\">2<\/span> <span class=\"hljs-number\">1<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u5176\u4e2d\uff0c<code>\"female\"<\/code>\u5bf9\u5e94\u7684\u6570\u5b57\u7f16\u7801\u662f1\uff0c<code>\"male\"<\/code>\u5bf9\u5e94\u7684\u6570\u5b57\u7f16\u7801\u662f2\u3002\u56e0\u5b50\u7684\u6c34\u5e73\u987a\u5e8f\u662f\u6309\u7167\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\u7684\u3002<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>levels()<\/code>\u51fd\u6570\u67e5\u770b\u56e0\u5b50\u7684\u6c34\u5e73\u3002\u4f8b\u5982\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-scss\"><span class=\"hljs-built_in\">levels<\/span>(gender)<br \/>\n<\/code><\/div>\n<\/div>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-csharp\">[<span class=\"hljs-meta\">1<\/span>] <span class=\"hljs-string\">\"female\"<\/span> <span class=\"hljs-string\">\"male\"<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<h2>\u56e0\u5b50\u7684\u5e94\u7528\u5b9e\u4f8b<\/h2>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u56e0\u5b50\u8fdb\u884c\u7edf\u8ba1\u5206\u6790\u7684\u4f8b\u5b50\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u6570\u636e\u96c6<code>df<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u7528\u6237\u7684\u6027\u522b\u3001\u5e74\u9f84\u548c\u6536\u5165\u7b49\u4fe1\u606f\u3002<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-less\"><span class=\"hljs-selector-tag\">df<\/span> &lt;<span class=\"hljs-selector-tag\">-<\/span> <span class=\"hljs-selector-tag\">data<\/span><span class=\"hljs-selector-class\">.frame<\/span>(<br \/>\ngender = <span class=\"hljs-built_in\">factor<\/span>(<span class=\"hljs-built_in\">c<\/span>(<span class=\"hljs-string\">\"male\"<\/span>, <span class=\"hljs-string\">\"female\"<\/span>, <span class=\"hljs-string\">\"male\"<\/span>, <span class=\"hljs-string\">\"male\"<\/span>, <span class=\"hljs-string\">\"female\"<\/span>)),<br \/>\nage = <span class=\"hljs-built_in\">c<\/span>(<span class=\"hljs-number\">25<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">35<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">45<\/span>),<br \/>\nincome = <span class=\"hljs-built_in\">c<\/span>(<span class=\"hljs-number\">5000<\/span>, <span class=\"hljs-number\">6000<\/span>, <span class=\"hljs-number\">7000<\/span>, <span class=\"hljs-number\">8000<\/span>, <span class=\"hljs-number\">9000<\/span>)<br \/>\n)<br \/>\n<\/code><\/div>\n<\/div>\n<h3>\u7edf\u8ba1\u5206\u6790<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>tapply()<\/code>\u51fd\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u7edf\u8ba1\u3002\u4f8b\u5982\uff0c\u7edf\u8ba1\u4e0d\u540c\u6027\u522b\u7684\u5e73\u5747\u6536\u5165\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-bash\">tapply(<span class=\"hljs-built_in\">df<\/span><span class=\"hljs-variable\">$income<\/span>, <span class=\"hljs-built_in\">df<\/span><span class=\"hljs-variable\">$gender<\/span>, mean)<br \/>\n<\/code><\/div>\n<\/div>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-yaml\"><span class=\"hljs-string\">female<\/span>   <span class=\"hljs-string\">male<\/span><br \/>\n<span class=\"hljs-number\">7500   <\/span><span class=\"hljs-number\">6666.67<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u5973\u6027\u7684\u5e73\u5747\u6536\u5165\u4e3a7500\uff0c\u7537\u6027\u7684\u5e73\u5747\u6536\u5165\u4e3a6666.67\u3002<\/p>\n<h3>\u53ef\u89c6\u5316<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>ggplot2<\/code>\u5305\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u6761\u5f62\u56fe\u5c55\u793a\u4e0d\u540c\u6027\u522b\u7684\u5e73\u5747\u6536\u5165\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"p-4 overflow-y-auto\">\n<p><code class=\"!whitespace-pre hljs language-kotlin\"><span style=\"background-color: #f2f4f5; font-family: Consolas, Monaco, monospace;\">library(ggplot2)<\/span><\/code><\/p>\n<pre class=\"corepress-code-pre\"><code>\r\nggplot(df, aes(x = gender, y = income)) +\r\ngeom_bar(stat = \"summary\", fun = \"mean\", fill = \"blue\")<\/code><\/pre>\n<p><code class=\"!whitespace-pre hljs language-kotlin\"><\/code><\/p>\n<\/div>\n<\/div>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<p>\u4ece\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u5973\u6027\u7684\u5e73\u5747\u6536\u5165\u9ad8\u4e8e\u7537\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>R\u8bed\u8a00\u4e2d\u7684\u56e0\u5b50\u662f\u4e00\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8e\u8868\u793a\u5206\u7c7b\u53d8\u91cf\u3002\u5728\u7edf\u8ba1\u5206\u6790\u4e2d\uff0c\u5206\u7c7b\u53d8\u91cf\u901a\u5e38\u662f\u6307\u79bb\u6563\u7684\uff0c\u6709\u9650\u7684\u53d6\u503c\u96c6\u5408\uff0c\u4f8b\u5982\u6027\u522b [&hellip;]<\/p>\n","protected":false},"author":111,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-388","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=388"}],"version-history":[{"count":0,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/388\/revisions"}],"wp:attachment":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}