[R] Assignment to variables fails to loop

Steve Murray smurray444 at hotmail.com
Fri Mar 27 19:11:20 CET 2009


Dear all,

I think I'm nearly there in writing R code which will read in files with two variable parts to the file name and then assigning these file names to objects, which also have two variable parts. I have got the code running without encountering errors, however, I receive 50+ of the same warnings:

1: In assign(paste("Fekete_", index$year, index$month, sep = ""),  ... :
  only the first element is used as variable name

And it's true, when I do ls() only Fekete198601 has been assigned. I've attempted to rectify this, but have only come up against further errors.

The code as it stands, is as follows:


# READ IN FILES FROM DISK

# File names have two variable parts: creating a 'file index' is a two-step process

index <- expand.grid(year = sprintf("%04d", seq(1986, 1995)), month = sprintf("%02d", 1:12))
filelist <- paste("C:\\Documents and Settings\\Data\\comp_runoff_hd_", paste(index$year, index$month, sep=''), '.asc', sep='')

filelist


# Assign file names to individual objects with variable name components

for (i in filelist) {
          assign(paste("Fekete_",index$year, index$month, sep=''),read.table(file=i, header=FALSE, sep=" "))
          

          update <- substr(i,35,55) # substring - 2nd argument is character at which extraction is to begin, 3rd argument is where extraction ends.
          print(c("LOADED FILE:",update), quote=FALSE)
          }


> ls()
[1] "Fekete_198601" "filelist"      "i"             "index"        
[5] "update" 


Why is it that only Fekete_198601 has had data assigned to it (there should be 120 such objects in total) and how do I go about solving this?


Many thanks again for any help offered,


Steve




More information about the R-help mailing list