[R] Row/columns names within 'assign' command

Steve Murray smurray444 at hotmail.com
Tue Mar 31 11:18:02 CEST 2009


Dear all,
 
I am attempting to add row and column names to a series of tables (120 in total) which have 2 variable parts to their name. These tables are created as follows:


# Create table indexes

index <- expand.grid(year = sprintf("%04d", seq(1986, 1995)), month = sprintf("%02d", 1:12))


# Read in and assign file names to individual objects with variable name components
 
for (i in seq(nrow(index))) {
		assign(paste("Fekete_",index$year[i], index$month[i], sep=''),
              read.table(file=paste("C:\\Data\\comp_runoff_hd_", index$year[i], index$month[i], ".asc", sep=""), header=FALSE, sep="")) 

# Create index of file names
files <- print(ls()[1:120], quote=FALSE)  # This is the best way I could manage to successfully attribute all the table names to a single list - I realise it's horrible coding (especially as it relies on the first 120 objects stored in the memory actually being the objects I want to use)...
 
files
  [1] "Fekete_198601" "Fekete_198602" "Fekete_198603" "Fekete_198604"
  [5] "Fekete_198605" "Fekete_198606" "Fekete_198607" "Fekete_198608"
  [9] "Fekete_198609" "Fekete_198610" "Fekete_198611" "Fekete_198612"
  [13] "Fekete_198701" "Fekete_198702" "Fekete_198703" "Fekete_198704"
  [17] "Fekete_198705" "Fekete_198706" "Fekete_198707" "Fekete_198708" ...[truncated - there are 120 in total]
 
 
# Provide column and row names according to lat/longs.
 
rnames <- sprintf("%.2f", seq(from = -89.75, to = 89.75, length = 360))
columnnames <- sprintf("%.2f", seq(from = -179.75, to = 179.75, length = 720))


for (i in 1:120) {
		Fekete_table <- get(paste("Fekete_", index$year[i], index$month[i], sep=''))
		colnames(Fekete_table) <- columnnames
                rownames(Fekete_table) <- rnames
		assign(paste("Fekete_",index$year[i], index$month[i], sep=''),
		colnames(Fekete_table))
		}

 
 
As you can see, I'm in a bit of a muddle during the column/row name assignments. In fact, this loop simply writes over the existing data in the tables and replaces it with all the column name values (whilst colnames remains NULL).
The problem I've been having is that I can't seem to tell R to assign these column/row heading values to the colnames/rownames within an assign command - it seems to result in errors even when I try breaking this assignment process down into steps.

How do I go about assigning rows and columns in this way, and how do I create a better way of indexing the file names?

Many thanks for any help offered,

Steve

_________________________________________________________________
 25GB of FREE Online Storage – Find out more




More information about the R-help mailing list