相关疑难解决方法(0)

使用R下载压缩数据文件,提取和导入数据

@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)

但我觉得我还有很长的路要走.有什么想法吗?

connection zip r

118
推荐指数
7
解决办法
11万
查看次数

解压缩tar.gz文件?

我想tar.gz在R中下载并打开以下文件:

http://s.wordpress.org/resources/survey/wp2011-survey.tar.gz

有没有可以实现这一目标的命令?

r unzip

28
推荐指数
1
解决办法
3万
查看次数

在R中自动读取zip文件

我需要自动化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)

compression r

24
推荐指数
2
解决办法
2万
查看次数

从shapefile中裁剪图

我正在尝试使用欧洲统计局提供的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()从图形设备获取坐标,但是插入值并没有提供想要的结果.

最小的例子

r spatial

3
推荐指数
1
解决办法
1741
查看次数

标签 统计

r ×4

compression ×1

connection ×1

spatial ×1

unzip ×1

zip ×1