[R] Read stata file from internet?

Michael Kubovy kubovy at virginia.edu
Sat Jan 26 12:41:23 CET 2008

Dear R-helpers,

I would like to have my students read into R an online Stata dataset: 'http://www.stat.ucla.edu/projects/datasets/risk_project.dta'

I was able to read it into R after downloading it and converting it  
with StatTransfer (http://www.stattransfer.com/).

Here is what happens when I use read.dta() as I would use read.table():

> require(foreign)
> risk2 <- read.dta('http://www.stat.ucla.edu/projects/datasets/risk_project.dta')
> Error in read.dta("http://www.stat.ucla.edu/projects/datasets/risk_project.dta 
> ") :
>   unable to open file

Then I try:

> riskTmp <- read.table('http://www.stat.ucla.edu/projects/datasets/risk_project.dta')
> Warning message:
> In read.table("http://www.stat.ucla.edu/projects/datasets/risk_project.dta 
> ") :
>   incomplete final line found by readTableHeader on 'http://www.stat.ucla.edu/projects/datasets/risk_project.dta'
> head(riskTmp)
>              V1
> 1 l\001\001\300
> 2        .%9.0g
> 3 \342CNTL%8.0g
> > risk2 <- read.dta(riskTmp)
> Error in read.dta(riskTmp) : first argument must be a file name

So I tried to save riskTmp as a file, and read it in using read.dta():

> save(riskTmp, file = 'risk.dta')
> risk2 <- read.dta('risk.dta')
> Error in read.dta("risk.dta") : not a Stata version 5-8 .dta file

But that presumably garbled the file. (Indeed, StatTransfer told me so.)

Would it be appropriate to request this as a feature of   
foreign::read.dta()? (Thomas Lumley is the author.)
Professor Michael Kubovy
University of Virginia
Department of Psychology
USPS:     P.O.Box 400400    Charlottesville, VA 22904-4400
Parcels:    Room 102        Gilmer Hall
         McCormick Road    Charlottesville, VA 22903
Office:    B011    +1-434-982-4729
Lab:        B019    +1-434-982-4751
Fax:        +1-434-982-4766
WWW:    http://www.people.virginia.edu/~mk9y/

More information about the R-help mailing list