{"id":826,"date":"2017-07-09T16:18:51","date_gmt":"2017-07-09T08:18:51","guid":{"rendered":"https:\/\/www.beneplot.com\/?p=826"},"modified":"2021-03-08T16:22:52","modified_gmt":"2021-03-08T08:22:52","slug":"r%e8%af%ad%e8%a8%80-excel%e6%95%b0%e6%8d%ae%e6%96%87%e4%bb%b6%e6%8b%86%e5%88%86%e4%b8%8e%e6%89%b9%e9%87%8f%e5%af%bc%e5%87%ba","status":"publish","type":"post","link":"https:\/\/www.beneplot.com\/?p=826","title":{"rendered":"R\u8bed\u8a00-Excel\u6570\u636e\u6587\u4ef6\u62c6\u5206\u4e0e\u6279\u91cf\u5bfc\u51fa"},"content":{"rendered":"<p><strong><span style=\"font-size: 16px;\">\u6709\u65f6\u9700\u8981\u6309\u7167\u4e00\u5b9a\u5b57\u6bb5\u5c06\u4e00\u4e2a\u6570\u636e\u6587\u4ef6\u62c6\u5206\u4e3a\u591a\u4e2a\uff0c<\/span><\/strong><\/p>\n<p><strong><span style=\"font-size: 16px;\">\u6709\u65f6\u9700\u8981\u4ee5Excel\u683c\u5f0f\u540c\u65f6\u8f93\u51fa\u3001\u4fdd\u5b58\u591a\u4e2a\u6570\u636e\u5206\u6790\u7ed3\u679c\uff0c<\/span><\/strong><\/p>\n<p><strong><span style=\"font-size: 16px;\">\u53ef\u7528R\u8bed\u8a00\u8fdb\u884c\u64cd\u4f5c\uff0c<\/span><\/strong><span style=\"font-size: 16px;\">\u5177\u4f53\u505a\u6cd5\u5982\u4e0b\uff1a<\/span><\/p>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>library(readxl)<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>library(xlsx)<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>library(dplyr)<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>data &lt;- read_excel(\"data.xlsx\")<\/code><\/span><\/strong><\/span><\/div>\n<div>\u00a0<\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>codee = unique(data$code)<\/code><\/span><\/strong><\/span><\/div>\n<div>\u00a0<\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code># dtname1 = paste(\"dt\",1:length(codee),sep = \"\")<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code># dtname2 = paste(\"dt_cor\",1:length(codee),sep = \"\")<\/code><\/span><\/strong><\/span><\/div>\n<div>\u00a0<\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>dtname1 = codee<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>dtname2 = paste(codee,\"_cor\",sep = \"\")\u00a0<\/code><\/span><\/strong><\/span><\/div>\n<div>\u00a0<\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code># for (i in c(1:length(codee))) {<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>#\u00a0 \u00a0assign( paste(\"dt\",i,sep = \"\"), data.frame(subset(data, data$code == codee[i])))<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code># }<\/code><\/span><\/strong><\/span><\/div>\n<div>\u00a0<\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>outPath = \"C:\\\\Users\\\\yangqie\\\\Desktop\\\\data\" ##\u8f93\u51fa\u8def\u5f84<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>out_fileName1 = sapply(dtname1,function(x){paste(x, \".xlsx\", sep='')}) ##.xlsx\u683c\u5f0f<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>out_filePath1\u00a0 = sapply(out_fileName1, function(x){paste(outPath ,x,sep='\\\\')}) ##\u8f93\u51fa\u8def\u5f84\u540d<\/code><\/span><\/strong><\/span><\/div>\n<div>\u00a0<\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>##\u8f93\u51fa\u6587\u4ef6 \u6309\u5b57\u6bb5\u5c06\u4e00\u6279\u6570\u636e\u5206\u89e3\u4e3a\u591a\u6279\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u547d\u540d<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>for(i in 1:length(codee)){<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>\u00a0 write.xlsx(assign(dtname1[i], data.frame(subset(data, data$code == codee[i]))),\u00a0<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0file=out_filePath1[i], row.name=T)\u00a0<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>}<\/code><\/span><\/strong><\/span><\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>##\u6309\u5b57\u6bb5\u5c06\u4e00\u6279\u6570\u636e\u5206\u89e3\u4e3a\u591a\u6279\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u547d\u540d<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>aaa = c()<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>for (i in c(1:length(codee))) {<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>\u00a0 aaa[[i]] = assign(dtname1[i], data.frame(subset(data, data$code == codee[i])))<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>}<\/code><\/span><\/strong><\/span><\/div>\n<div>\u00a0<\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code># for (i in c(1:length(codee))) {<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>#\u00a0 \u00a0assign(paste(\"cor_dt\",i,sep = \"\"), data.frame(cor(aaa[[i]][4:length(data)])))<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code># }<\/code><\/span><\/strong><\/span><\/div>\n<div>\u00a0<\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>out_fileName2 = sapply(dtname2,function(x){paste(x, \".xlsx\", sep='')}) ##.xlsx\u683c\u5f0f<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>out_filePath2\u00a0 = sapply(out_fileName2, function(x){paste(outPath ,x,sep='\\\\')}) ##\u8f93\u51fa\u8def\u5f84\u540d<\/code><\/span><\/strong><\/span><\/div>\n<div>\u00a0<\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>##\u540c\u65f6\u5bf9\u591a\u6279\u6570\u636e\u8fdb\u884c\u76f8\u5173\u5206\u6790\uff0c\u5e76\u8f93\u51fa\u591a\u4e2a\u7ed3\u679c\u6587\u4ef6<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>for(i in 1:length(codee)){<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>\u00a0 write.xlsx(assign(dtname2[i], data.frame(cor(aaa[[i]][4:length(data)]))),\u00a0<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0file=out_filePath2[i], row.name=T)\u00a0<\/code><\/span><\/strong><\/span><\/div>\n<div><span style=\"font-family: 'arial black', sans-serif;\"><strong><span style=\"font-size: 20px;\"><code>}<\/code><\/span><\/strong><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u65f6\u9700\u8981\u6309\u7167\u4e00\u5b9a\u5b57\u6bb5\u5c06\u4e00\u4e2a\u6570\u636e\u6587\u4ef6\u62c6\u5206\u4e3a\u591a\u4e2a\uff0c \u6709\u65f6\u9700\u8981\u4ee5Excel\u683c\u5f0f\u540c\u65f6\u8f93\u51fa\u3001\u4fdd\u5b58\u591a\u4e2a\u6570\u636e\u5206\u6790\u7ed3\u679c\uff0c \u53ef\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.beneplot.com\/?p=826\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"R\u8bed\u8a00-Excel\u6570\u636e\u6587\u4ef6\u62c6\u5206\u4e0e\u6279\u91cf\u5bfc\u51fa - Beneplot\" \/>\n<meta property=\"og:description\" content=\"\u6709\u65f6\u9700\u8981\u6309\u7167\u4e00\u5b9a\u5b57\u6bb5\u5c06\u4e00\u4e2a\u6570\u636e\u6587\u4ef6\u62c6\u5206\u4e3a\u591a\u4e2a\uff0c \u6709\u65f6\u9700\u8981\u4ee5Excel\u683c\u5f0f\u540c\u65f6\u8f93\u51fa\u3001\u4fdd\u5b58\u591a\u4e2a\u6570\u636e\u5206\u6790\u7ed3\u679c\uff0c \u53ef\u7528 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.beneplot.com\/?p=826\" \/>\n<meta property=\"og:site_name\" content=\"Beneplot\" \/>\n<meta property=\"article:published_time\" content=\"2017-07-09T08:18:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-03-08T08:22:52+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.beneplot.com\/#website\",\"url\":\"https:\/\/www.beneplot.com\/\",\"name\":\"Beneplot\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.beneplot.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-CN\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.beneplot.com\/?p=826#webpage\",\"url\":\"https:\/\/www.beneplot.com\/?p=826\",\"name\":\"R\\u8bed\\u8a00-Excel\\u6570\\u636e\\u6587\\u4ef6\\u62c6\\u5206\\u4e0e\\u6279\\u91cf\\u5bfc\\u51fa - Beneplot\",\"isPartOf\":{\"@id\":\"https:\/\/www.beneplot.com\/#website\"},\"datePublished\":\"2017-07-09T08:18:51+00:00\",\"dateModified\":\"2021-03-08T08:22:52+00:00\",\"author\":{\"@id\":\"https:\/\/www.beneplot.com\/#\/schema\/person\/ea14f85ae789ceaaa712ceee1dd1f95b\"},\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.beneplot.com\/?p=826\"]}]},{\"@type\":[\"Person\"],\"@id\":\"https:\/\/www.beneplot.com\/#\/schema\/person\/ea14f85ae789ceaaa712ceee1dd1f95b\",\"name\":\"beneplot\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.beneplot.com\/#personlogo\",\"inLanguage\":\"zh-CN\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/15d96ce801cfddbd59ef2b0d986cd9b1?s=96&r=g\",\"caption\":\"beneplot\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/posts\/826"}],"collection":[{"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=826"}],"version-history":[{"count":2,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/posts\/826\/revisions"}],"predecessor-version":[{"id":828,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/posts\/826\/revisions\/828"}],"wp:attachment":[{"href":"https:\/\/www.beneplot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}