DataScience+ We share R tutorials from scientists at academic and scientific institutions with a goal to give everyone in the world access to a free knowledge. Our tutorials cover different topics including statistics, data manipulation and visualization!
Getting Data

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

Sometimes you may want to export data from R to another application. Often R users prefer to export their data from R to TXT file, because the TXT file is easy imported on other applications. However, most used statical applications are SPSS and Stata, so here we will show how you to export data as .sps and .dta file.

Here an example code, dt is the name of dataframe in R, and mydata is the name of file you want to save.

Exporting data to txt:

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

Exporting data to csv:

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

Exporting data to spss, with loading first package foreign:

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

Rxporting data to stata:

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

If you have questions, post comment below.