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

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:

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

(4) Exporting data to Stata:

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

(5) Export data to SAS:

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

Hope you find this short post useful.

KD
Author
Klodian Dhana

Creator of DataScience+, a home for R and Python enthusiasts to share their knowledge with the world since 2015. I also publish tutorials in R and the tidyverse on the topics I find most interesting.

27 articles on DataScience+
View all posts

7 Comments

  1. FZ
    Farid Zamani March 7, 2019

    Hi, I need your help.
    here is my code :
    library(stsm)
    pars <- c(var1 = 1, var2 = 0.0001,var3 = 0.05, var4 = 0.08)
    m <- stsm.model(model = "local-level", y = ts(seq(200), frequency = 52),
    pars = pars, nopars = NULL)
    ss <- char2numeric(m)
    set.seed(123)
    y <- datagen.stsm(n = 144, model = list(Z = ss$Z, T = ss$T, H = ss$H, Q = ss$Q),
    n0 = 20, freq = 52, old.version = TRUE)$data
    print(y)

    my question is, how to tabulate the data into one column only or export the data to txt or csv file?

    Thank you.

    Reply
  2. DQ
    David Ato Quansah June 21, 2017

    fantastic. Thanks

    Reply
  3. ES
    Emily Stone April 30, 2017

    JK I figured it out apologies

    Reply
  4. J
    John May 19, 2016

    I have some stock price data required from the “quantmod” package, however, when I export it i do not get the dates only the values. How could you solve this? im using write.foreign and exporting it to sps

    Reply
    1. K
      Klodian May 20, 2016

      You get values in SPSS, or in R?

      Reply
      1. J
        John May 20, 2016

        I have the data in r that looks like this

        2007-01-01 -0.07
        2007-01-02 -0.06

        from the code

        require(quantmod)

        SP <- getSymbols("^GSPC", from=as.Date("2007-01-01"), to=as.Date("2016-01-01"), auto.assign=FALSE)

        SP$p <- log(SP$GSPC.Adjusted) ## log() computes the natural logaritm

        SP$r <- SP$p – lag(SP$p) ## lag() computes the lag 1

        But when I export it to txt and sps it looks like this

        -0.07
        -0.04… etc

        So only the values and not the date label is extracted

        Reply
        1. K
          Klodian May 20, 2016

          When I transport from R to SPSS with using write.foreign i get values instead of dates in SPSS. I can simply change that by going to Variable View, and change the type from numeric to data in SPSS.

          Reply

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.