R-help readers,
I''m having a problem with an R script (see below), which regularly
generates the error message,
Error in start:(start + (sample.length - 1)) :
NA/NaN argument
, for which I am unsure of the cause.
In essence, the script (below) generates the start and end points for random
subsamples from along a vector (in reality a transect (of a given length,
sample.length), that may be used to subsample from another vector of data. The
important point is that once a section of the transect has
been sampled, it cannot be resampled by an overlapping sub-transect. WHile
there are obvious limits on the number of non-overlapping subvectors that can be
taken from the original vector, I don''t think this is the source of the
error message, although the error is increasingly common as more of hte original
vector is sampled.
Any help would be greatly appreciated.
regards
Nick
r1<-function(transect.length,sample.length,reps) { #Input: transect length,
subsample length No.subsamples (reps).
x<-c(1:transect.length) #create transect, x, of required length.
subsamples<-mat.or.vec(reps,2) #create matrix for output of subsampled
start and end points.
for(i in 1:reps) { #start loop for reps.
start<-ceiling(runif(1,0,(transect.length-sample.length))) #generate
initial random start point.
while (any(x[start:(start+(sample.length-1))])=="NA")
start<-sample(x[1:(transect.length-sample.length)],1) #check for
Na''s in the subsample range indicating previously sampled points. if
present, choose a new start point.
subsamples[i,1]<-start #store start point in ouput matrix
subsamples[i,2]<-(start+(sample.length-1)) #store end point in output matrix
x[start:(start+(sample.length-1))]<-NA #convert sampled points to NA, for
detection within the while loop above.
}
return(x,subsamples) #return output.
}
I would typically run the script with an input such as r1(1000,50,10)
[[alternate HTML version deleted]]
Nick Bond wrote:
> R-help readers,
>
> I'm having a problem with an R script (see below), which regularly
generates the error message,
>
> Error in start:(start + (sample.length - 1)) :
> NA/NaN argument
>
> , for which I am unsure of the cause.
>
> In essence, the script (below) generates the start and end points for
random subsamples from along a vector (in reality a transect (of a given
length, sample.length), that may be used to subsample from another
vector of data. The important point is that once a section of the
transect has
> been sampled, it cannot be resampled by an overlapping sub-transect.
WHile there are obvious limits on the number of non-overlapping
subvectors that can be
> taken from the original vector, I don't think this is the source of
the error message, although the error is increasingly common as more of
hte original vector is sampled.
> Any help would be greatly appreciated.
> regards
> Nick
>
> r1<-function(transect.length,sample.length,reps) { #Input: transect
length, subsample length No.subsamples (reps).
> x<-c(1:transect.length) #create transect, x, of required length.
> subsamples<-mat.or.vec(reps,2) #create matrix for output of
subsampled start and end points.
> for(i in 1:reps) { #start loop for reps.
> start<-ceiling(runif(1,0,(transect.length-sample.length)))
#generate initial random start point.
> while (any(x[start:(start+(sample.length-1))])=="NA")
If x is numeric then you should use is.na to test for missing values:
while(any(is.na(x[start:(start+(sample.length-1))])))
> subsamples[i,1]<-start #store start point in ouput matrix
> subsamples[i,2]<-(start+(sample.length-1)) #store end point in
output matrix
> x[start:(start+(sample.length-1))]<-NA #convert sampled points to
NA, for detection within the while loop above.
> }
> return(x,subsamples) #return output.
> }
>
> I would typically run the script with an input such as r1(1000,50,10)
>
I didn't run this so I'm not sure if my suggestion is the only problem
with the script.
Regards,
Sundar