@EZGraphs在Twitter上写道:"很多在线csv都是压缩的.有没有办法下载,解压缩档案,并使用R?#Rstats将数据加载到data.frame"
我今天也试图这样做,但最终只是手动下载zip文件.
我尝试过类似的东西:
fileName <- "http://www.newcl.org/data/zipfiles/a1.zip"
con1 <- unz(fileName, filename="a1.dat", open = "r")
Run Code Online (Sandbox Code Playgroud)
但我觉得我还有很长的路要走.有什么想法吗?
我需要自动化R来读取一个zip文件中的csv数据文件.
例如,我会输入:
read.zip(file = "myfile.zip")
Run Code Online (Sandbox Code Playgroud)
在内部,将要做的是:
myfile.zip到临时文件夹read.csv如果zip文件中有多个文件,则会引发错误.
我的问题是获取包含在zip文件中的文件的名称,在orded中提供它来执行read.csv命令.有谁知道怎么做?
UPDATE
这是我根据@Paul答案写的函数:
read.zip <- function(zipfile, row.names=NULL, dec=".") {
# Create a name for the dir where we'll unzip
zipdir <- tempfile()
# Create the dir using that name
dir.create(zipdir)
# Unzip the file into the dir
unzip(zipfile, exdir=zipdir)
# Get the files into the dir
files <- list.files(zipdir)
# Throw an error if there's more than one
if(length(files)>1) stop("More than one data file inside zip") …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用欧洲统计局提供的shapefile和数据生成Choroplete地图.shapefile已在此处下载:使用此帖子中的 JD Longs代码.
这是重现下面发布的图的最小代码.
library(maptools)
tmpdir <- tempdir()
url <- 'http://ec.europa.eu/eurostat/cache/GISCO/geodatafiles/NUTS_2010_03M_SH.zip'
file <- basename(url)
download.file(url, file)
unzip(file, exdir = tmpdir )
shapeFile <- paste(tmpdir,"/Shape/data/NUTS_RG_03M_2010", sep="")
EU <- readShapeSpatial(shapeFile)
plot(EU)
Run Code Online (Sandbox Code Playgroud)
我的问题是,我希望情节区域只关注欧洲,但由于海外地区(法国和西班牙),情节没有正确的焦点.在上面的例子中,有一种简单的方法可以"裁剪"绘图区域吗?
我想要摆脱的多边形是"Country_Shape"的一部分,所以过滤它们是没有选择的.我试图通过在plot命令中定义xlim和ylim参数来实现我的目标,但没有成功.我使用locator()从图形设备获取坐标,但是插入值并没有提供想要的结果.
