[R] How to download and unzip data in a loop

Alexandra Catena amc5981 at gmail.com
Wed Feb 4 23:24:36 CET 2015


Hi All,

I need to loop through and download the past 10 years of met data to a
temporary directory.  I then need to unzip it and place it into another
directory.


year = (2005:2015)

for (i in year)
  tmpdir = tempdir()
  file[i] = file.path(tmpdir, sprintf('724927-23285-%4i.gz', i))
  url = sprintf('
ftp://ftp.ncdc.noaa.gov/pub/data/noaa/%4i/724927-23285-%4i.gz', i, i)
  #file = basename(url)
  download.file(url, file[i])
  files = dir(tmpdir, '*.gz', full.names=FALSE)
  read.table(gzfile('files'))



'file' returns 2015 indices with "/tmp/RtmpKvB4Wz/724927-23285-2015.gz"
next to 2015. and files returns 724927-23285-2015.gz.  However, when I try
to unzip the gz file using the last line, it says it cannot open the
connection and the probable reason is that there is no such file or
directory.



Thanks,
Alexandra

	[[alternative HTML version deleted]]



More information about the R-help mailing list