Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read Stata 13 file in R

Tags:

r

stata

Is there a way to read a Stata version 13 dataset file in R?

I have tried to do the following:

> library(foreign) > data = read.dta("TEAdataSTATA.dta")  

However, I got an error:

Error in read.dta("TEAdataSTATA.dta") :
not a Stata version 5-12 .dta file

Could someone point out if there is a way to fix this?

like image 434
kolonel Avatar asked May 27 '14 21:05

kolonel


People also ask

How do I read a Stata File in R?

To import SPSS, Stata, or SAS data files in R, first install and load the package foreign . The functions read. spss() , read. dta() , and read.

Can R read Stata code?

The answer is “yes!, R can read Stata (. dta) files. This is easy to do with the Haven package. First, load the package: library(haven) .

How do I open a Stata File?

Select File > Open... or click on the Open button and navigate to the file. Select File > Recent Datasets > filename. Type use filename in the Command window. Stata will look for filename in the current working directory.

What are .DTA files?

Description. The Stata_dta format (with extension . dta) is a proprietary binary format designed for use as the native format for datasets with Stata, a system for statistics and data analysis. Stata 1.0 was released in 1985 for the IBM PC. Stata is now available for Windows, Mac OS, and Unix.


2 Answers

There is a new package to import Stata 13 files into a data.frame in R.

Install the package and read a Stata 13 dataset with read.dta13():

install.packages("readstata13")  library(readstata13) dat <- read.dta13("TEAdataSTATA.dta") 

Update: readstata13 imports in version 0.8 also files from Stata 6 to 14

More about the package: https://github.com/sjewo/readstata13

like image 80
sjewo Avatar answered Sep 16 '22 14:09

sjewo


There's a new package called Haven, by Hadley Wickham, which can load Stata 13 dta files (as well as SAS and SPSS files)

library(haven) # haven package now available on cran df <- read_dta('c:/somefile.dta') 

See: https://github.com/hadley/haven

like image 26
yoyoyoyosef Avatar answered Sep 18 '22 14:09

yoyoyoyosef