Announcement

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

  • Import R-data into Stata

    Dear all,

    I have a data set produced in R called (test.Rdata) and would like to import into Stata. The data set was produced in R-in Windows. My idea was to read the data first using R in my Mac (OSX: High sierra, version: 10.13.6) (R-version: R 3.5.1) and use R-foreign package to write the data file in Stata-format and then read through Stata. I have tried with the R-foreign package in my R but getting error message (please see below the R-syntax). Can anyone please possibly help with what I am doing wrong here.



    Code:
    setwd("/Users/kirk/Desktop/My_files/") #directory set up
    
    > library (foreign)
    
    > write.dta(test,"Mydata.dta")
    
    Error in is.data.frame(dataframe) : object 'test' not found
    Stata version: 15.2


    Roman

  • #2
    I'm sure it can be done in R. But, you may just want to check out this website. It can handle a lot of things and it is easy to use.

    https://gallery.shinyapps.io/rioweb/
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    Stata Version: 17.0 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      Thanks Richard. No luck in this too. The file uploads okay and then showing this message after selecting "Stata" from the list and clicking download button: "File not found. An error occurred during a connection to gallery.shinyapps.io.".


      Roman

      Comment


      • #4
        Does the file work ok in R? You are sure it isn't corrupted?
        -------------------------------------------
        Richard Williams, Notre Dame Dept of Sociology
        Stata Version: 17.0 MP (2 processor)

        EMAIL: [email protected]
        WWW: https://www3.nd.edu/~rwilliam

        Comment


        • #5
          It does not show any error message when I load it with load function (I am very fundamental with R).:

          Code:
          //R-command:
          
          load("test.RData")
          And then if I want to see the variables with the following command, it shows NULL! :

          Code:
          names("test")
          NULL
          But the size of the dataset shows 18megabyte!
          Last edited by Roman Mostazir; 10 Sep 2018, 19:39. Reason: Adding further information
          Roman

          Comment


          • #6
            Try

            write.dta (test, file="mydata.dta")


            ​​​​

            Comment


            • #7
              Thanks Dario. I think the file is corrupt. It is showing the same error.

              Code:
              > write.dta (test, file="mydata.dta")
              Error in is.data.frame(dataframe) : object 'test' not found
              Thanks again for your help.
              Roman

              Comment


              • #8
                Maybe not. May this help?


                https://stackoverflow.com/questions/...uld-i-fix-this

                Comment


                • #9
                  This is not a Stata question. You question refers to a problem in using R -- thus you should first try to solve the R problem, perhaps through the R-Help mailing list (although the list is not as friendly and helpful as the Stata Forum).

                  I don't know whether I understand
                  I am very fundamental with R
                  correctly: Do you have fundamental knowledge or not? At any rate, you should take the R error message seriously: R does not recognize 'test' as being a dataframe. As a start you can try
                  Code:
                  names(test)
                  str(test)
                  to locate the problem. Do all columns of the dataframe have names? Does the structure of the dataframe seem to be OK?

                  I will not follow this thread further -- try the R-Help mailing list.

                  Comment


                  • #10
                    Re: Dirk Enzmann

                    I think this does become a Stata question when is framed as: "How do you read a .Rdata file in Stata?". Indeed, Roman stated the question in a relevant way for this forum: "Import R-data into Stata".

                    Comment


                    • #11
                      It is rarely best practice to try to read a binary file that is native to one software package into a different package. Typically, it is very straightforward to transfer data between different software packages using an interchange format. In this case I would recommend the .csv format. Just go into R and output the data set as a .csv file with write.csv, then read the data into Stata with insheet using.

                      Comment


                      • #12
                        Oh, and in R, don't forget to set the missing string to the empty string, otherwise you'll get a bunch of "NA" strings in Stata. These can be difficult to work with.

                        Code:
                        write.csv(..., na="")

                        Comment

                        Working...
                        X