Skip to content
Prev 167735 / 398502 Next

download/retain text file structure with RCurl/getURL(): Solution

Dear list,
I'm posting the solution to my problem in case others may find this useful. This code was sent to me by Phil Spector. With a bit of cleaning, it can easily be converted to a usable format. Thanks to Gabor Grothendieck, David winsemius and Martin Morgan for also sending possible solutions. Thank you all for taking the time to help, I would not have solved this on my own.

###############################################
require(RCurl)
txtfile = getURL('ftp://ftp.wcc.nrcs.usda.gov/data/snow/snow_course/table/history/idaho/13e19.txt',ftp.use.epsv = FALSE)
txtvec = strsplit(txtfile,'\n')[[1]]
widths = c(4,rep(c(5,4,6),6))
res = read.fwf(textConnection(txtvec[9:65]),widths=widths,stringsAsFactors=FALSE)
nums = c(3,4,6,7,9,10,12,13,15,16,18,19)
res[,nums] = sapply(res[,nums],as.numeric)
################################################

Best,

Zack

----------------------------------------