DataScience+ An online community for showcasing R & Python tutorials. It operates as a networking platform for data scientists to promote their talent and get hired. Our mission is to empower data scientists by bridging the gap between talent and opportunity.
Getting Data

Exporting Data from R to TXT, CSV, SPSS or Stata

  • Published on August 1, 2015 at 8:51 pm
  • Updated on October 30, 2017 at 3:37 pm

Sometimes you may want to export your data from R (.Rdata) to another format, such as TXT file (a tab-delimited text file) and CSV file (comma separated values file). However, most used statistical software are SAS, Stata, and SPSS, so here we will show how you to export data to several formats.

In the code below, dt is the name of data in R, and mydata new data name.

(1) Exporting data to TXT (Tab Delimited Text File):

write.table(dt, "mydata.txt", sep=",")

(2) Exporting data to CSV:

write.table(dt, file="mydata.csv",sep=",",row.names=F)

(3) Exporting data to SPSS. Here is necessary to install foreing package:

write.foreign(dt, "mydata.txt", "mydata.sps",   package="SPSS")

(4) Exporting data to Stata:

write.dta(dt, "mydata.dta")

(5) Export data to SAS:

write.foreign(dt, "mydata.txt", "", package="SAS")

Hope you find this short post useful.