{"id":5979,"date":"2016-03-08T13:29:29","date_gmt":"2016-03-08T12:29:29","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=5979"},"modified":"2016-03-08T13:29:29","modified_gmt":"2016-03-08T12:29:29","slug":"tutorial-exporting-an-r-data-frame-to-a-csv-file","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=5979","title":{"rendered":"Tutorial: Exporting an R data frame to a .csv file"},"content":{"rendered":"<p>Say you have two vectors in R (These are taken from my tutorial <a href=\"http:\/\/Simple nonlinear least squares curve fitting in R\">Simple nonlinear least squares curve fitting in R<\/a>)<\/p>\n<pre>xdata = c(-2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9)\r\nydata = c(0.699369,0.700462,0.695354,1.03905,1.97389,2.41143,1.91091,0.919576,-0.730975,-1.42001)\r\n<\/pre>\n<p>We put these in a data frame with<\/p>\n<pre>data = data.frame(xdata=xdata,ydata=ydata)\r\n<\/pre>\n<p>This looks like this in R<\/p>\n<pre>   xdata     ydata\r\n1  -2.00  0.699369\r\n2  -1.64  0.700462\r\n3  -1.33  0.695354\r\n4  -0.70  1.039050\r\n5   0.00  1.973890\r\n6   0.45  2.411430\r\n7   1.20  1.910910\r\n8   1.64  0.919576\r\n9   2.32 -0.730975\r\n10  2.90 -1.420010\r\n<\/pre>\n<p>Exporting to a .csv file is done using the standard R function, <strong>write.csv<\/strong><\/p>\n<pre>write.csv(data,file='example_data.csv')\r\n<\/pre>\n<p>The resulting .csv file looks like this:<\/p>\n<pre>\"\",\"xdata\",\"ydata\"\r\n\"1\",-2,0.699369\r\n\"2\",-1.64,0.700462\r\n\"3\",-1.33,0.695354\r\n\"4\",-0.7,1.03905\r\n\"5\",0,1.97389\r\n\"6\",0.45,2.41143\r\n\"7\",1.2,1.91091\r\n\"8\",1.64,0.919576\r\n\"9\",2.32,-0.730975\r\n\"10\",2.9,-1.42001\r\n<\/pre>\n<p>I don&#8217;t want to include the row numbers in my output. To achieve this, we do<\/p>\n<pre>write.csv(data,file='example_data.csv',row.names=FALSE)\r\n<\/pre>\n<p>This gets us a file that looks like this:<\/p>\n<pre> \"xdata\",\"ydata\"\r\n-2,0.699369\r\n-1.64,0.700462\r\n-1.33,0.695354\r\n-0.7,1.03905\r\n0,1.97389\r\n0.45,2.41143\r\n1.2,1.91091\r\n1.64,0.919576\r\n2.32,-0.730975\r\n2.9,-1.42001\r\n<\/pre>\n<p>I can also remove the quotes around xdata and ydata with <strong>quote=FALSE<\/strong><\/p>\n<pre>write.csv(data,file='example_data.csv',row.names=FALSE,quote=FALSE)\r\n<\/pre>\n<p>giving the file below<\/p>\n<pre>xdata,ydata\r\n-2,0.699369\r\n-1.64,0.700462\r\n-1.33,0.695354\r\n-0.7,1.03905\r\n0,1.97389\r\n0.45,2.41143\r\n1.2,1.91091\r\n1.64,0.919576\r\n2.32,-0.730975\r\n2.9,-1.42001\r\n<\/pre>\n<p><strong>Changing the separator<\/strong><\/p>\n<p>Despite the fact that they are asking R to write a <strong>comma<\/strong> separated file, some people try to change the separator. Perhaps you&#8217;d like to try changing it to a tab for example. The following looks reasonable:<\/p>\n<pre>write.csv(data,file='example_data.csv',row.names=FALSE,quote=FALSE,sep=\"\\t\")\r\n<\/pre>\n<p>Although it understands what you are trying to do, R will completely ignore your request!<\/p>\n<pre>Warning message:\r\nIn write.csv(data, file = \"example_data.csv\", row.names = FALSE,  :\r\n  attempt to set 'sep' ignored\r\n<\/pre>\n<p>This is because <strong>write.csv<\/strong> is designed to ensure that some standard .csv conventions are followed. It&#8217;s trying to protect you against yourself!<\/p>\n<p>In the UK, the convention for .csv files is to use . for a decimal point and , as a separator and that&#8217;s the convention that <strong>write.csv<\/strong> sticks to.\u00a0Other countries have a different convention &#8211; \u00a0they\u00a0use a , for the decimal point and a ; for the separator. The function <strong>write.csv2<\/strong> takes care of that for you.<\/p>\n<p>If you absolutely must change the separator to something else, make use of <strong>write.table<\/strong> instead:<\/p>\n<pre>write.table(data,file='example_data.csv',row.names=FALSE,quote=FALSE,sep=\"\\t\")<\/pre>\n<p>Now, the file will come out like this:<\/p>\n<pre>xdata   ydata\r\n-2      0.699369\r\n-1.64   0.700462\r\n-1.33   0.695354\r\n-0.7    1.03905\r\n0       1.97389\r\n0.45    2.41143\r\n1.2     1.91091\r\n1.64    0.919576\r\n2.32    -0.730975\r\n2.9     -1.42001\r\n<\/pre>\n<p>Further reading: <a href=\"https:\/\/stat.ethz.ch\/R-manual\/R-devel\/library\/utils\/html\/write.table.html\">Official write.table documentation in R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Say you have two vectors in R (These are taken from my tutorial Simple nonlinear least squares curve fitting in R) xdata = c(-2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9) ydata = c(0.699369,0.700462,0.695354,1.03905,1.97389,2.41143,1.91091,0.919576,-0.730975,-1.42001) We put these in a data frame with data = data.frame(xdata=xdata,ydata=ydata) This looks like this in R xdata ydata 1 -2.00 0.699369 2 -1.64 0.700462 3 -1.33 0.695354 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7,36],"tags":[],"class_list":["post-5979","post","type-post","status-publish","format-standard","hentry","category-programming","category-r"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-1yr","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5979","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5979"}],"version-history":[{"count":2,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5979\/revisions"}],"predecessor-version":[{"id":5982,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5979\/revisions\/5982"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}