English 中文(简体)
从有多个 R 文件夹的已拉链临时文件读取 csv 文件
原标题:read csv file from zipped temp file with multiple folders in R
  • 时间:2012-05-24 01:04:09
  •  标签:
  • r
  • csv
  • unzip

我试图读取我从网络中提取的文件中包含的 csv 文件。 问题是, 被拉链的文件有多个串联文件夹。 我对多个不同的单位必须这样做, 所以我要执行环绕 。 循环没有问题, 文件名正确, 并可以下载文件 。 但是, 我收到错误信息( 我想是因为 R 找不到要它找到的确切文件 ) 。 错误是 :

Error in open.connection(file, "rt") : cannot open the connection
In addition: Warning message:
In open.connection(file, "rt") :
  cannot locate file  XXXX.csv  in zip file  c:yyy	empla 


download.file(paste("http://web.com_",units[i],"_",places[j],".zip",
                     sep=""),
                     temp,
                     cacheOK = F )
data <- read.csv2(unz(temp,
                   paste("name_",units[i],"_",places[j],".csv",
                   sep="")),
                   header=F,
                   skip=1)
unlink(temp)
fili<-rbind(X,
            data)

}

我怎么让R找到我想要的文件?

问题回答

你有正确的方法,但(警告告诉你的)文件名错了。

值得重复检查, 在您开始读取文件内容之前, 是否存在拉链文件 。

if(file.exists(temp))
{
  read.csv2(unz(...))
} else
{
  stop("ZIP file has not been downloaded to the place you expected.")
}

也可以在下载文件内部浏览(您可能希望先解开它), 以确保您正在正确的地方查找 CSV 内容 。

看起来您要读的文件似乎位于目录中。 在此情况下, 您的读取应该修改如下 :

data <- read.csv2(unz(temp,
                   paste("**dirname**/name_",units[i],"_",places[j],".csv",
                   sep="")),
                   header=F,
                   skip=1)




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签