{"id":180110,"date":"2024-05-08T20:28:27","date_gmt":"2024-05-08T12:28:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/180110.html"},"modified":"2024-05-08T20:28:38","modified_gmt":"2024-05-08T12:28:38","slug":"r%e8%af%ad%e8%a8%80%e9%87%8c%ef%bc%8c%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e6%95%b0%e6%8d%ae%e9%9b%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/180110.html","title":{"rendered":"R\u8bed\u8a00\u91cc\uff0c\u5982\u4f55\u521b\u5efa\u6570\u636e\u96c6"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27065221\/f53a4914-91a4-42e8-9d7d-e9c23834f926.webp\" alt=\"R\u8bed\u8a00\u91cc\uff0c\u5982\u4f55\u521b\u5efa\u6570\u636e\u96c6\" \/><\/p>\n<p><p>\u5728R\u8bed\u8a00\u4e2d\uff0c\u521b\u5efa\u6570\u636e\u96c6\u53ef\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528<code>data.frame()<\/code>\u3001<code>matrix()<\/code>\u3001<code>list()<\/code>\u3001\u4ee5\u53ca<code>read.table()<\/code><strong>\u6216\u5176\u4ed6\u6587\u4ef6\u5bfc\u5165\u51fd\u6570\u3002\u800c<\/strong><code>data.frame()<\/code>\u51fd\u6570\u662f\u521b\u5efa\u6570\u636e\u96c6\u7684\u6700\u5e38\u89c1\u65b9\u6cd5\uff0c\u5b83\u5c06\u4e0d\u540c\u7684\u6570\u636e\u5217\u7ec4\u5408\u6210\u4e00\u4e2a\u8868\u683c\uff0c\u5728\u6570\u636e\u5206\u6790\u4e2d\u5e94\u7528\u5e7f\u6cdb\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528DATA.FRAME()<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u6570\u636e\u96c6\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>data.frame()<\/code>\u51fd\u6570\u3002\u6570\u636e\u6846\u662fR\u4e2d\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8eExcel\u4e2d\u7684\u8868\u683c\uff0c\u5176\u4e2d\u6bcf\u5217\u7684\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u4e0d\u540c\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u521b\u5efa\u7b80\u5355\u6570\u636e\u6846\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">my_data &lt;- data.frame(<\/p>\n<p>  Column1 = c(1, 2, 3, 4),<\/p>\n<p>  Column2 = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;),<\/p>\n<p>  Column3 = c(TRUE, FALSE, TRUE, FALSE)<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u91cc\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>my_data<\/code>\u7684\u6570\u636e\u6846\uff0c\u5b83\u542b\u6709\u4e09\u5217\uff0c\u5217\u7684\u6570\u636e\u5206\u522b\u662f\u6570\u5b57\u3001\u5b57\u7b26\u548c\u903b\u8f91\u578b\u3002\u4f7f\u7528<code>data.frame()<\/code>\u662f\u521b\u5efa\u6570\u636e\u96c6\u7684\u7075\u6d3b\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u4e0d\u540c\u5217\u6709\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528MATRIX()<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u53ea\u542b\u6570\u503c\u7c7b\u578b\u6570\u636e\u7684\u4e8c\u7ef4\u7ed3\u6784\u53ef\u4ee5\u4f7f\u7528<code>matrix()<\/code>\u51fd\u6570\u3002\u77e9\u9635\uff08matrix\uff09\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u62e5\u6709\u76f8\u540c\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">my_matrix &lt;- matrix(<\/p>\n<p>  c(1, 2, 3, 4, 5, 6),<\/p>\n<p>  nrow = 2,<\/p>\n<p>  ncol = 3,<\/p>\n<p>  byrow = TRUE<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u67092\u884c3\u5217\u7684\u77e9\u9635\uff0c<code>byrow = TRUE<\/code>\u8868\u793a\u6570\u636e\u662f\u6309\u884c\u586b\u5145\u7684\u3002\u82e5\u8bbe\u4e3aFALSE\uff0c\u5219\u6570\u636e\u5c06\u6309\u5217\u586b\u5145\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7LIST()<\/h3>\n<\/p>\n<p><p>\u5728R\u4e2d\uff0c\u5217\u8868\uff08list\uff09\u662f\u4e00\u79cd\u5bf9\u8c61\uff0c\u5141\u8bb8\u60a8\u521b\u5efa\u4e00\u4e2a\u6709\u5e8f\u7684\u96c6\u5408\uff0c\u5176\u4e2d\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u4f8b\u5982\u6570\u503c\u3001\u5b57\u7b26\u3001\u5411\u91cf\u3001\u77e9\u9635\u751a\u81f3\u53e6\u4e00\u4e2a\u5217\u8868\u3002\u521b\u5efa\u5217\u8868\u7684\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">my_list &lt;- list(<\/p>\n<p>  Numbers = 1:5,<\/p>\n<p>  Characters = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;),<\/p>\n<p>  Bools = c(TRUE, FALSE, TRUE)<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5217\u8868\u662f\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7279\u522b\u9002\u5408\u5c06\u4e0d\u540c\u957f\u5ea6\u548c\u7c7b\u578b\u7684\u6570\u636e\u7ec4\u5408\u6210\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6587\u4ef6\u5bfc\u5165\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u76f4\u63a5\u5728R\u4e2d\u521b\u5efa\u6570\u636e\u96c6\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u5916\u90e8\u6587\u4ef6\u6765\u521b\u5efa\u3002R\u8bed\u8a00\u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\u6765\u5bfc\u5165\u5916\u90e8\u6570\u636e\uff0c\u4f8b\u5982\u4f7f\u7528<code>read.table()<\/code>\u3001<code>read.csv()<\/code>\u3001<code>read.xlsx()<\/code>\u7b49\uff0c\u6839\u636e\u4e0d\u540c\u7684\u6587\u4ef6\u7c7b\u578b\u9009\u7528\u4e0d\u540c\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-r\">my_data_from_file &lt;- read.csv(&quot;path\/to\/your\/filename.csv&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u901a\u8fc7<code>read.csv()<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\u521b\u5efa\u6570\u636e\u96c6\u3002\u9002\u5f53\u65f6\uff0c\u4f7f\u7528\u6587\u4ef6\u5bfc\u5165\u51fd\u6570\u53ef\u4ee5\u8282\u7701\u65f6\u95f4\u5e76\u51cf\u5c11\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u6570\u636e\u96c6\u662f\u6570\u636e\u5206\u6790\u7684\u524d\u63d0\uff0cR\u8bed\u8a00\u63d0\u4f9b\u4e86\u7075\u6d3b\u591a\u6837\u7684\u65b9\u6cd5\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u9700\u6c42\u3002\u4e86\u89e3\u5e76\u638c\u63e1\u5404\u79cd\u521b\u5efa\u548c\u7ba1\u7406\u6570\u636e\u96c6\u7684\u6280\u5de7\u5bf9\u4e8e\u8fdb\u884c\u9ad8\u6548\u7684\u6570\u636e\u5206\u6790\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u95ee\u98981\uff1a\u5982\u4f55\u5728R\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u7a7a\u767d\u6570\u636e\u96c6\uff1f<\/strong><\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>data.frame()<\/code>\u51fd\u6570\u5728R\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u7a7a\u767d\u7684\u6570\u636e\u96c6\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u4efb\u4f55\u884c\u6216\u5217\u7684\u6570\u636e\u96c6\uff1a<\/p>\n<pre><code>my_dataset &lt;- data.frame()\n<\/code><\/pre>\n<p><strong>\u95ee\u98982\uff1a\u5982\u4f55\u5728R\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u7279\u5b9a\u884c\u548c\u5217\u7684\u6570\u636e\u96c6\uff1f<\/strong><\/p>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u7279\u5b9a\u884c\u548c\u5217\u7684\u6570\u636e\u96c6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>data.frame()<\/code>\u51fd\u6570\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u5217\u6307\u5b9a\u521d\u59cb\u503c\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u5177\u67093\u5217\u548c5\u884c\u7684\u6570\u636e\u96c6\uff1a<\/p>\n<pre><code>my_dataset &lt;- data.frame(col1 = c(1, 2, 3, 4, 5),\n                         col2 = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;),\n                         col3 = c(TRUE, FALSE, TRUE, FALSE, TRUE))\n<\/code><\/pre>\n<p><strong>\u95ee\u98983\uff1a\u5982\u4f55\u5728R\u8bed\u8a00\u4e2d\u4ece\u73b0\u6709\u6587\u4ef6\u5bfc\u5165\u6570\u636e\u96c6\uff1f<\/strong><\/p>\n<p>\u8981\u4ece\u73b0\u6709\u7684\u6587\u4ef6\u4e2d\u5bfc\u5165\u6570\u636e\u96c6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>read.csv()<\/code>\u51fd\u6570\uff08\u5982\u679c\u6587\u4ef6\u662f\u4ee5\u9017\u53f7\u5206\u9694\u7684\uff09\u6216<code>read.table()<\/code>\u51fd\u6570\uff08\u5982\u679c\u6587\u4ef6\u5206\u9694\u7b26\u4e0d\u662f\u9017\u53f7\uff09\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code>my_dataset &lt;- read.csv(&quot;file.csv&quot;)  # \u5bfc\u5165\u4ee5\u9017\u53f7\u5206\u9694\u7684\u6587\u4ef6\n<\/code><\/pre>\n<pre><code>my_dataset &lt;- read.table(&quot;file.txt&quot;, sep = &quot;\\t&quot;)  # \u5bfc\u5165\u4f7f\u7528\u5236\u8868\u7b26\u5206\u9694\u7684\u6587\u4ef6\n<\/code><\/pre>\n<p>\u8bf7\u66ff\u6362<code>file.csv<\/code>\u548c<code>file.txt<\/code>\u4e3a\u4f60\u8981\u5bfc\u5165\u7684\u5b9e\u9645\u6587\u4ef6\u540d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728R\u8bed\u8a00\u4e2d\uff0c\u521b\u5efa\u6570\u636e\u96c6\u53ef\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528data.frame()\u3001matrix()\u3001list() [&hellip;]","protected":false},"author":3,"featured_media":180123,"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\/180110"}],"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=180110"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180110\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/180123"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=180110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=180110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=180110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}