[R] indeterminate for loop
lm609 at york.ac.uk
lm609 at york.ac.uk
Tue Mar 15 15:14:21 CET 2011
Hello,
I have written a 'for' loop which on the first run makes nearest neighbour
calculations for my dataset 'A' in relation to dataset 'B', then based on
these results, some of the rows from A are moved into dataset B, and the
calculation is repeated on the remaining rows in A. Therefore a smaller and
smaller amount of data is analysed as the loop proceeds, since A is losing
rows each time. The results of the calculation change as less data is used,
but not in a predictable way, therefore the end point of the loop is
indeterminate.
The script is long and the datasets big, but a simplified example where 'A'
and 'B' are datasets, and the function 'ndist2' calculates 'x' is...
for (i in 1:100)
{
C <- data.frame(ndist2(B,A))
C1 <- subset(C, x = min(C$x))
B <- rbind(B, C1)
A <- subset(C, x > min(C$x))
}
So eventually A runs out.
Currently, I let the loop run until a warning comes up which effectively
says no more data, and at this point it is finished.
What I would now like to do is place this loop inside another loop in order
to run through multiple datasets, but I can't do this when the script
produces a warning message at the end of each dataset.
Is there any way I can overcome this problem?
Thanks for your help,
Louise
More information about the R-help
mailing list