[R] "array" and "which" functions

Kumudan cybermails at gmail.com
Thu Feb 5 00:07:13 CET 2009


Hi all,

I have a data frame of coordinates, "coord". I need an "output"
(array) where the number of rows = number of rows in "coord".
I am trying to use the "which" function to extract indices from the
coordinates with the following code:

d=2
nd=length(coord[,1])
output=list(array)
...
...
which(xcoord <i+d & xcoord >i-d & ycoord <i+d & ycoord >i-d)
...
return(output)


I believe the dots in the above code need to be filled by some loop
function. Suggestions with sample code would be greatly appreciated.
Below are the coordinates for the "coord" file.

         xcoord     ycoord
   [1,]  0.00000 16.09400
   [2,]  0.80470 16.09400
   [3,]  0.80470 16.89870
   [4,]  0.80470 17.70340
   [5,]  1.60940 16.09400
   [6,]  1.60940 16.89870
   [7,]  2.41410 14.48460
   [8,]  2.41410 15.28930
   [9,]  2.41410 16.09400
  [10,]  2.41410 16.89870
  [11,]  3.21880 14.48460
  [12,]  3.21880 15.28930
  [13,]  3.21880 16.09400
  [14,]  3.21880 16.89870
  [15,]  4.02350 14.48460
  [16,]  4.02350 15.28930
  [17,]  4.02350 16.09400
  [18,]  4.02350 16.89870
  [19,]  4.82820 14.48460
  [20,]  4.82820 15.28930
  [21,]  4.82820 16.09400
  [22,]  4.82820 16.89870
  [23,]  5.63290 14.48460
  [24,]  5.63290 15.28930
  [25,]  5.63290 16.09400
  [26,]  5.63290 16.89870
  [27,]  6.43760 14.48460
  [28,]  6.43760 15.28930
  [29,]  6.43760 16.09400
  [30,]  6.43760 16.89870
  [31,]  7.24230 13.67990
  [32,]  7.24230 14.48460
  [33,]  7.24230 15.28930
  [34,]  7.24230 16.09400
  [35,]  8.04700 13.67990
  [36,]  8.04700 14.48460
  [37,]  8.04700 15.28930
  [38,]  8.04700 16.09400
  [39,]  8.04700 16.89870
  [40,]  8.85170 13.67990
  [41,]  8.85170 14.48460
  [42,]  8.85170 15.28930
  [43,]  8.85170 16.09400
  [44,]  9.65640 13.67990
  [45,]  9.65640 14.48460
  [46,]  9.65640 15.28930
  [47,]  9.65640 16.09400
  [48,]  9.65640 16.89870
  [49,] 10.46110 15.28930
  [50,] 10.46110 16.09400


The "output" should look something like this:

[[1]]
 [1] 2, 5, 8, 10

[[2]]
 [1] 3, 4, 6, 9, 11, 13

........
[[50]]
 [1] 41, 45, 49


Thanks in advance.

Sincerely,
Kumudan




More information about the R-help mailing list