[R] Find downstream values in a network

Jon Olav Skoien jon.skoien at jrc.ec.europa.eu
Wed Mar 2 15:28:54 CET 2011


Dear list,

I have a data.frame with segments between river junctions and 
dimensionless predictions of runoff (runoff/area) at some of these 
junctions. As I want to plot my values on a continuous river network 
(this data.frame is part of a SpatialLinesDataFrame), I would like to 
change NA values to the closest non-NA value downstream.

Here is a simple example:
 > examp = data.frame(FROMJCT = c(1,2,3,4,5,7,8,9,10,11,12,13,14),TOJCT 
= c(2,3,4,5,6,4,7,8,8,10,8,12,9))
 > examp$pred = NA
 > examp$pred[c(2,4,5,7,13)] = c(1,2,3,4,5)
 > examp
    FROMJCT TOJCT pred
1        1     2   NA
2        2     3    1
3        3     4   NA
4        4     5    2
5        5     6    3
6        7     4   NA
7        8     7    4
8        9     8   NA
9       10     8   NA
10      11    10   NA
11      12     8   NA
12      13    12   NA
13      14     9    5

"FROMJCT" describes the upstream and "TOJCT" the downstream junction. 
examp$pred[7] above should hence get the value 3, as its "TOJCT" 
junction is the same as the "FROMJCT" junction of examp$pred[6]. 
examp$pred[8] should get the same value, as it is linked to 
examp$pred[6] through examp$pred[7]. I can do this iteratively by 
propagating values upwards in the river network by combining a while and 
a for-loop:

ichange = 1
while (ichange > 0) {
   ichange = 0
   for (i in 1:dim(examp)[1]) {
     if (!is.na(examp$pred[i])) {
       toid = which(examp$TOJCT == examp$FROMJCT[i])
       if (length(toid) > 0 && is.na(examp$pred[toid])) {
         examp$pred[toid] = examp$pred[i]
         ichange = ichange + 1
       }
     }
   }
   print(ichange)
}


But this looks messy and is rather slow when the river network is 
described through a large number of segments. I am quite sure that I 
have missed a better way of propagating the values. This is a 
preprocessing step before plotting a result in a documentation example, 
so I am looking for a short, intuitive and nice solution... Any hints?

Thanks,
Jon



More information about the R-help mailing list