{"id":5603,"date":"2025-05-17T09:21:46","date_gmt":"2025-05-17T01:21:46","guid":{"rendered":"http:\/\/cnliutz.pgrm.cc\/?p=5603"},"modified":"2025-05-17T09:21:47","modified_gmt":"2025-05-17T01:21:47","slug":"r%e8%af%ad%e8%a8%80%e6%95%b0%e6%8d%ae%e6%a1%86%ef%bc%88datafram%ef%bc%89%e6%95%b0%e6%8d%ae%e7%9a%84%e7%ae%80%e5%8d%95%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"http:\/\/cnliutz.wicp.vip\/?p=5603","title":{"rendered":"R\u8bed\u8a00\u6570\u636e\u6846\uff08datafram\uff09\u6570\u636e\u7684\u7b80\u5355\u5206\u6790"},"content":{"rendered":"\n<p>\u4ee3\u7801\u5b8c\u6210\u7684\u529f\u80fd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u521b\u5efa\u6570\u636e\u96c6\uff08\u6570\u636e\u6846\uff09\uff0c<\/li>\n\n\n\n<li>\u5199\u5165\u5230\u6587\u4ef6\u4e2d\uff0c<\/li>\n\n\n\n<li>\u663e\u793a\u6570\u636e\uff0c<\/li>\n\n\n\n<li>\u5206\u7ec4\u8ba1\u7b97\u5e73\u5747\u5e74\u9f84\uff0c<\/li>\n<\/ul>\n\n\n\n<p>\u5728Rstudio\u4e2d\uff0c\u521b\u5efaR markdown\u6216R notebook\u6587\u4ef6\u8fd0\u884c\u3002\u4ee5\u4e0b\u662f\u6dfb\u52a0\u4e86\u6ce8\u91ca\u7684\u5b8c\u6574R\u4ee3\u7801\uff0c\u89e3\u91ca\u4e86\u6bcf\u4e2a\u6b65\u9aa4\u7684\u529f\u80fd\u548c\u76ee\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># R Notebook\u6587\u6863\u8bbe\u7f6e\ntitle: \"R Notebook\"\noutput: html_notebook\n\n# \u521b\u5efa\u793a\u4f8b\u6570\u636e\u96c6\nage = c(22,25,45,33,55)       # \u521b\u5efa\u5e74\u9f84\u5411\u91cf\nname = c(\"Ken\",\"Jhon\",\"Daliu\",\"Lisa\",\"Jack\")  # \u521b\u5efa\u59d3\u540d\u5411\u91cf\ngender = c(\"M\",\"F\",\"F\",\"M\",\"F\")  # \u521b\u5efa\u6027\u522b\u5411\u91cf(M=\u7537,F=\u5973)\narea = c(\"City\",\"Rural\",\"City\",\"Rural\",\"Rural\")  # \u521b\u5efa\u533a\u57df\u5411\u91cf\ndf &lt;- data.frame(area,name,gender,age)  # \u7ec4\u5408\u5411\u91cf\u4e3a\u6570\u636e\u6846\n\n# \u5c06\u6570\u636e\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\nwrite.table(df,file = \"~\/python\/name_age.csv\",  # \u6307\u5b9a\u4fdd\u5b58\u8def\u5f84\n            row.names = FALSE,     # \u4e0d\u4fdd\u5b58\u884c\u540d\n            col.names = TRUE,      # \u4fdd\u5b58\u5217\u540d\n            quote = FALSE)         # \u4e0d\u4f7f\u7528\u5f15\u53f7\u5305\u88f9\u6587\u672c\n\n# \u663e\u793a\u6570\u636e\u6846\u5185\u5bb9\u5e76\u7ed8\u5236\u5e74\u9f84\u8d8b\u52bf\u56fe\ndf  # \u6253\u5370\u6570\u636e\u6846\u5185\u5bb9\nplot(df&#91;&#91;\"age\"]],ylab = \"age\",type = \"l\")  # \u7ed8\u5236\u5e74\u9f84\u6298\u7ebf\u56fe\nprint(\"file -name_age.csv- saved\")  # \u63d0\u793a\u6587\u4ef6\u5df2\u4fdd\u5b58\n\n# \u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u67e5\u770b\u6570\u636e\u7ed3\u6784\nlibrary(readr)  # \u52a0\u8f7dreadr\u5305\u7528\u4e8e\u9ad8\u6548\u8bfb\u53d6\u6570\u636e\nname_age &lt;- read_csv(\"~\/python\/name_age.csv\")  # \u8bfb\u53d6CSV\u6587\u4ef6\nView(name_age)  # \u5728RStudio\u4e2d\u67e5\u770b\u6570\u636e\ntypeof(name_age)  # \u67e5\u770b\u6570\u636e\u7c7b\u578b\n\n# \u751f\u6210\u6027\u522b\u4e0e\u533a\u57df\u7684\u4ea4\u53c9\u8868\ntable(area,gender)  # \u7edf\u8ba1\u4e0d\u540c\u533a\u57df\u548c\u6027\u522b\u7684\u4eba\u6570\u5206\u5e03\n\n# \u65b9\u6cd51: \u4f7f\u7528\u57fa\u7840\u51fd\u6570tapply\u8ba1\u7b97\u4e0d\u540c\u6027\u522b\u5e73\u5747\u5e74\u9f84\navg_age_by_gender &lt;- tapply(df$age, df$gender, mean)\nprint(avg_age_by_gender)\n\n# \u65b9\u6cd52: \u4f7f\u7528dplyr\u5305\u8ba1\u7b97\u4e0d\u540c\u6027\u522b\u5e73\u5747\u5e74\u9f84\nlibrary(dplyr)  # \u52a0\u8f7ddplyr\u5305\navg_age_by_gender &lt;- df %&gt;%\n  group_by(gender) %&gt;%  # \u6309\u6027\u522b\u5206\u7ec4\n  summarise(mean_age = mean(age, na.rm = TRUE))  # \u8ba1\u7b97\u5e73\u5747\u5e74\u9f84\u5e76\u5904\u7406\u7f3a\u5931\u503c\nprint(avg_age_by_gender)\n\n# \u65b9\u6cd53: \u4f7f\u7528data.table\u5305\u8ba1\u7b97\u4e0d\u540c\u6027\u522b\u5e73\u5747\u5e74\u9f84(\u9002\u5408\u5927\u6570\u636e\u96c6)\nlibrary(data.table)  # \u52a0\u8f7ddata.table\u5305\ndt &lt;- as.data.table(df)  # \u5c06\u6570\u636e\u6846\u8f6c\u6362\u4e3adata.table\u683c\u5f0f\navg_age_by_gender &lt;- dt&#91;, .(mean_age = mean(age, na.rm = TRUE)), by = gender]  # \u5206\u7ec4\u8ba1\u7b97\nprint(avg_age_by_gender)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u529f\u80fd\u8bf4\u660e<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u521b\u5efa\u4e0e\u4fdd\u5b58<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u521b\u5efa\u4e86\u5305\u542b\u59d3\u540d\u3001\u5e74\u9f84\u3001\u6027\u522b\u548c\u533a\u57df\u7684\u793a\u4f8b\u6570\u636e\u96c6<\/li>\n\n\n\n<li>\u5c06\u6570\u636e\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u5230\u6307\u5b9a\u8def\u5f84<\/li>\n\n\n\n<li>\u7ed8\u5236\u4e86\u5e74\u9f84\u8d8b\u52bf\u6298\u7ebf\u56fe<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u8bfb\u53d6\u4e0e\u5206\u6790<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528<code>readr<\/code>\u5305\u8bfb\u53d6CSV\u6587\u4ef6<\/li>\n\n\n\n<li>\u67e5\u770b\u4e86\u6570\u636e\u7684\u7ed3\u6784\u548c\u7c7b\u578b<\/li>\n\n\n\n<li>\u751f\u6210\u4e86\u6027\u522b\u4e0e\u533a\u57df\u7684\u4ea4\u53c9\u8868<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5206\u7ec4\u7edf\u8ba1<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>tapply\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528R\u57fa\u7840\u51fd\u6570\u8fdb\u884c\u5206\u7ec4\u8ba1\u7b97<\/li>\n\n\n\n<li><strong>dplyr\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528\u7ba1\u9053\u64cd\u4f5c\u7b26\u8fdb\u884c\u6d41\u5f0f\u6570\u636e\u5904\u7406<\/li>\n\n\n\n<li><strong>data.table\u65b9\u6cd5<\/strong>\uff1a\u9488\u5bf9\u5927\u6570\u636e\u96c6\u7684\u9ad8\u6548\u5904\u7406\u65b9\u6848<\/li>\n<\/ul>\n\n\n\n<p>\u6240\u6709\u7edf\u8ba1\u65b9\u6cd5\u90fd\u8003\u8651\u4e86\u7f3a\u5931\u503c\u5904\u7406\uff08\u901a\u8fc7<code>na.rm=TRUE<\/code>\u53c2\u6570\uff09\uff0c\u53ef\u4ee5\u6839\u636e\u6570\u636e\u89c4\u6a21\u548c\u4e2a\u4eba\u504f\u597d\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee3\u7801\u5b8c\u6210\u7684\u529f\u80fd\uff1a \u5728Rstudio\u4e2d\uff0c\u521b\u5efaR markdown\u6216R notebo <span class=\"readmore\"><a href=\"http:\/\/cnliutz.wicp.vip\/?p=5603\">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-5603","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5603","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=5603"}],"version-history":[{"count":1,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5603\/revisions"}],"predecessor-version":[{"id":5604,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5603\/revisions\/5604"}],"wp:attachment":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5603"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}