【发布时间】:2019-01-25 11:18:45
【问题描述】:
我想直接从 R 中下载并打开以下包含月度和年度消费者价格指数的 Excel 文件。
https://www.bfs.admin.ch/bfsstatic/dam/assets/7066959/master (链接可以在本站找到:https://www.bfs.admin.ch/bfs/de/home/statistiken/preise/landesindex-konsumentenpreise/lik-resultate.assetdetail.7066959.html)
我以前使用浏览器手动下载此文件,将其保存在本地计算机上,然后用 R 打开 xlsx 文件并毫无问题地处理数据。
我现在尝试直接从 R 中读取文件,但到目前为止没有运气。从上面的 URL 可以看出,没有 .xlsx 扩展名等,所以我认为文件以某种方式压缩。这是我到目前为止所尝试的以及我遇到的问题。
library(foreign)
library(xlsx)
# in a browser, this links opens or dowloads an xlsx file
likurl <- "https://www.bfs.admin.ch/bfsstatic/dam/assets/7066959/master"
temp <- tempfile()
download.file(likurl, temp)
list.files <- unzip(temp,list=TRUE)
data <- read.xlsx(unz(temp,
+ list.files$Name[8]), sheetIndex=2)
最后一步的结果是
Error in +list.files$Name[8] : invalid argument to unary operator
我不太了解 unz 函数,但是在阅读 unz 的帮助文件时可以看出这是某种错误(我在网上某处找到了这个建议的解决方案)。
我还尝试了以下不同的方法:
library(XLConnect)
likurl <- "https://www.bfs.admin.ch/bfsstatic/dam/assets/7066959/master"
tmp = tempfile(fileext = ".xlsx")
download.file(likurl, tmp)
readWorksheetFromFile(tmp, sheet = 2, startRow = 4,
colNames = TRUE, rowNames = FALSE)
最后一行作为结果返回:
Error: ZipException (Java): invalid entry size (expected 1644 but got 1668 bytes)
在将数据从 excel 读入 R 时,我将非常感谢有关如何打开这些数据并像往常一样使用它的任何帮助。 提前非常感谢!
【问题讨论】:
-
也许这个会有所帮助:stackoverflow.com/questions/41368628/…
-
感谢您的提示,这有帮助!
-
酷。我很高兴它有所帮助。