可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Is there a way to read a Stata 13 file in R?
I tried the following, and I got the following error:
> library(foreign) > data = read.dta("TEAdataSTATA.dta") 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
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)
# install.packages("devtools") devtools::install_github("hadley/haven") require(haven) df
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!!!!