簡體   English   中英

在R中讀取一個zip文件

[英]Read a zip file in 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'

我不知道為什么它無法讀取數據,希望有人可以幫助我!

這適用於所有idv文件,但idv1似乎已損壞。 您需要使用其他工具解壓縮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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM