Read Stata 13 file in R

2019-01-07 17:23发布

问题:

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?

回答1:

If you have Stata 13, then you can load it there and save it as a Stata 12 format using the command saveold (see help saveold). Afterwards, take it to R.

If you have, Stata 10 - 12, you can use the user-written command use13, (by Sergiy Radyakin) to load it and save it there; then to R. You can install use13 running ssc install use13.

Details can be found at http://radyakin.org/transfer/use13/use13.htm

Other alternatives, still with Stata, involve exporting the Stata format to something else that R will read, e.g. text-based files. See help export within Stata.

Update

Starting Stata 14, saveold has a version() option, allowing one to save in Stata .dta formats as old as Stata 11.



回答2:

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



回答3:

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



回答4:

In the meanwhile savespss command became a member of the SSC archive and can be installed to Stata with: findit savespss

The homepage http://www.radyakin.org/transfer/savespss/savespss.htm continues to work, but the program should be installed from the SSC now, not from the beta location.



回答5:

I had the same problem. Tried read.dta13, read.dta but nothing worked. Then tried the easiest and least expected: MS Excel! It opened marvelously. I saved it as a .csv and used in R!!! Hope this helps!!!!



回答6:

I am not familiar with the current state of R programs regarding their ability to read other file formats, but if someone doesn't have Stata installed on their computer and R cannot read a specific version of Stata's dta files, Pandas in Python can now do the vast majority of such conversions.

Basically, the data from the dta file are first loaded using the pandas.read_stata function. As of version 0.23.0, the supported encoding and formats can be found in a related answer of mine.

Then one can either save the data as a csv file and import them using standard R functions, or instead use the pandas.DataFrame.to_feather function, which exports the data using a serialization format built on Apache Arrow. The latter has extensive support in R as it was conceived to promote interoperability with Pandas.



标签: r stata