[R] object 'xxx' not found

Duncan Murdoch murdoch at stats.uwo.ca
Tue Feb 9 00:49:39 CET 2010


emorway wrote:
> The following line of code seems fairly straight forward, yet it is kicking
> back an error message:
> for (i in
> 1:length(mean.natveg.frac)){month.observed[i]=as.numeric(names(mean.natveg.frac[i]))%%12}
>
> Error message:
>
> Error in month.observed[i] = as numeric(names(mean.natveg.frac[i]))%%12 :
> object 'month.observed' not found
>
> Seems like its not found because I'm trying to define it.  Also
> length(mean.natveg.frac) = 103

You can't index the object before it is defined.  You can assign to 
non-existent elements of an object, but it is very inefficient.  So the 
best fix is to put the line

month.observed <- numeric(length(mean.natveg.frac))

before the for loop.

Duncan Murdoch



More information about the R-help mailing list