阅读为r的zip文件

问题描述:

我想读从网上一个zip文件,我的代码,随后阅读为r的zip文件

temp<-tempfile() 
download<-download.file("http://depts.washington.edu/control/LARRY/TE/IDVs/idv1.zip",temp) 
data<-read.table(unz(temp,"r.dat"),head=FALSE) 
unlink(temp) 

但它显示了一个错误

Error in open.connection(file, "rt") : cannot open the connection 
In addition: Warning message: 
In open.connection(file, "rt") : 
    cannot locate file 'r.dat' in zip file 'C:\Users\CHENGF~2\AppData\Local\Temp\RtmpgtJShr\file361c5d0a55eb' 

我不知道为什么它无法读取数据,希望有人能帮助我!

这适用于除了idv1似乎已损坏的所有idv文件。您需要使用另一种工具解压缩idv1.zip并将其读入...

readrdat <- function(n) { 
    fname <- paste0("idv",n) 
    zipname <- paste0(fname,".zip") 
    weblink <- paste0("http://depts.washington.edu/control/LARRY/TE/IDVs/",zipname) 
    download.file(weblink,zipname) 
    data <- read.table(unz(zipname,paste0(fname,"/r.dat")),header=FALSE) 
    unlink(zipname) 
    return(data) 
} #readrdat 

lsdata <- lapply(1:15, function(n) { 
    tryCatch(readrdat(n), error=function(e) NULL) 
}) 
lapply(lsdata, is.null)