[R] Read stata file from internet?

Frank E Harrell Jr f.harrell at vanderbilt.edu
Sat Jan 26 21:01:38 CET 2008


Henrique Dallazuanna wrote:
> Try this:
> 
> tmpfile <- tempfile()
> download.file('http://www.stat.ucla.edu/projects/datasets/risk_project.dta',
> tmpfile)
> risk2 <- read.dta(tmpfile)
> unlink(tmpfile)

The stata.get function in the Hmisc package will automatically fetch 
from http://... and provides other features such as conversion of dates 
and using variable labels.

Frank

> 
> 
> On 26/01/2008, Michael Kubovy <kubovy at virginia.edu> wrote:
>> 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/
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> 


-- 
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University



More information about the R-help mailing list