Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Export data to RDS files

    Hello,
    For my work, I am using a combination of Stata and R. I would like to export my data from Stata saving it to an ". rds" format, in order to keep the labels of my variables once I import everything bak into R.

    Is there any way to do this? thanks a lot for your help


    Best regards



  • #2
    Hi Ale,

    The only way I know to retain Stata meta-data about variables that do not have a base R analog (e.g., dataset labels, variable labels, extended missing values, and display formats) is to use the -haven- package with the read_dta() function.

    From there, you can use saveRDS() to store the R object and all the meta-data will be stored as well.

    Have to be careful as you work with the data in R though. Many R functions drop object attributes like labels that -haven- stores thus to be able to back-translate into Stata and have the write_dta() function store the labels and other meta-data correctly, they have to be retained. This is not easy to do and I cannot advise on how to do this just to warn that it is easy to lose these object attributes when working in R as the language does not prioritize retaining them as does Stata.

    - joe
    Last edited by Joseph Luchman; 15 Dec 2020, 07:53. Reason: Added note about saving in R data serialization/RDS form
    Joseph Nicholas Luchman, Ph.D., PStatĀ® (American Statistical Association)
    ----
    Research Fellow
    Fors Marsh

    ----
    Version 18.0 MP

    Comment


    • #3
      Dear Joseph,
      thanks a lot for your reply, but unfortunately I fear it does not address my issue. Indeed, I have a ".dta" file that works great in Stata 15, but using haven to import it into data produces the following error message: Failed to parse /Users/folder/my_data.dta: Unable to allocate memory. I have also tried to export it as a delimited file using
      Code:
      export delimited
      It has a problem in formatting the very long strings and loses a lot of information once I re-imported into R - the data contains strL or str#, #>244.

      Is there any other export format that I could try for my file in order to safely import it into R? Alternatively do you know what may cause the error in haven

      thanks a lot in advance for your help

      Comment


      • #4
        I dont know about different export formats but I would try export to excel and then use the excel file to import into R. In my experience Excel export has been very robust....

        Comment


        • #5
          Thanks oscar for the suggestion, the issue with your suggested approach is that it only works for a small dataset that excel can handle, for very large datasets I do not think it works

          Comment

          Working...
          X