Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a dta file in R

Tags:

r

stata

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.

like image 272
J. Perez Avatar asked May 23 '16 13:05

J. Perez


People also ask

How do I open a DTA file?

Double-click on a Stata data file, which is a file whose extension is . dta. Note: The file extension may not be visible, depending on what options you have set in your operating system. Select File > Open... or click on the Open button and navigate to the file.

What package is read DTA in R?

dta files into R is the readstata13 package, which, despite what the name suggests, can read Stata 13 and Stata 14 files.

How do I load a dataset in R?

If you look at the package listing in the Packages panel, you will find a package called datasets. Simply check the checkbox next to the package name to load the package and gain access to the datasets. You can also click on the package name and RStudio will open a help file describing the datasets in this package.


1 Answers

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.

like image 107
coffeinjunky Avatar answered Sep 28 '22 10:09

coffeinjunky