Search code examples
rstata

Open a dta file in R


I am trying to open a Stata .dta file which is compressed into winrar in R. Here are my codes:

library(foreign)
setwd("C:/Users/ASUS/Desktop/Data on oil/Oil discovery")
data <- read.dta("oil_discovery")

and I get :

Error in read.dta("oil_discovery") : unable to open file: 'No such file or directory'

I think that my problem is coming from the assignment of my working directory but I don't know how to manage it.


Solution

  • You need to specify the full file name to read.dta. This includes the file ending. That is, instead of

     data <- read.dta("oil_discovery")
    

    you need to write

     data <- read.dta("oil_discovery.dta")
    

    If there is an additional problem with the compression, I would imagine that the error message will be different. However, Error in read.dta("oil_discovery") : unable to open file: 'No such file or directory' very explicitly points out that the current error is that the file oil_discovery is not found.

    A good way to check if the name or path is causing the error is to use choose.files(). That is, run the following line:

     data <- read.dta(choose.files())
    

    This will open a pop-up window where you can manually select the file. If this works, then the name of the file was misspecified.