[R] Isolating a column within a loop

Henrique Dallazuanna wwwhsd at gmail.com
Mon Feb 1 15:02:11 CET 2010


Try this:

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],
sep=''))[,3]))
}


On Mon, Feb 1, 2010 at 11:49 AM, Steve Murray <smurray444 at hotmail.com> wrote:
>
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list