Open a dta file in R
Asked Answered
I

2

6

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.

Identic answered 23/5, 2016 at 13:43 Comment(2)
Does read.dta() promise to decompress?Eamon
Try rio::import github.com/leeper/rioSmoothspoken
H
10

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.

Hyder answered 23/5, 2016 at 13:51 Comment(1)
If you are trying to open a file from Stata versions >12, this solution won't work. But there is an other approach here.Nippers
C
0
library(haven)

data <- read_dta("**.dta")

View(data)

Complicated answered 3/4, 2022 at 7:37 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Kazoo

© 2022 - 2024 — McMap. All rights reserved.