[R] Isolating a column within a loop

Steve Murray smurray444 at hotmail.com
Mon Feb 1 14:49:47 CET 2010


Dear all,

I am trying to calculate the mean of one column for many data frames. The code I am using is as follows:

> for (i in seq(nrow(index))) {
        assign(paste(model, "_mean_",index$year[i], index$month[i], sep=''),
              mean(get(paste(model, index$year[i], index$month[i], "[,3]", sep=''))))
         }

Error in get(paste(model, index$year[i], index$month[i], "[,3]", sep = "")) : 
  object 'cccma207101[,3]' not found

The error message I'm getting is strange, because this object does exist:

str(cccma207101[,3])
 num [1:61538] 0.687 2.661 0 0 0 ...

If I leave the "[,3]" out of the loop, the code seems to work fine - so I'm isolating this as the cause of the problem for now.

My question is therefore, how can I use the above code to extract column three from each of the (360) data frames?

Many thanks for any help offered,

Steve

 		 	   		  
_________________________________________________________________
Got a cool Hotmail story? Tell us now



More information about the R-help mailing list