[R] subset point data set by distance ranges

Charles C. Berry cberry at tajo.ucsd.edu
Wed Dec 10 06:12:59 CET 2008

```On Tue, 9 Dec 2008, David M Warner wrote:

>
>
>   Greetings
>   I'm using R 2.8 with recent (last month) versions of the packages I need to
>   use at present.
>
>   I'm interested in examining hierarchical spatio-temporal patterns in a data
>   set.  The data consist of 94 points (X, Y, UTM coordinates) at which catch
>   rates  for  a  fish were recorded and there are also estimates of prey
>   available for these fish at the same locations.  I have reason to believe
>   that the relationships between predators and prey varies with spatial scale
>   (nested processes).
>
>   To test this hypothesis, I'd like to generate subsets of the points that are
>   separated by distance ranges (1-50 km, 51-100 km, etc) so I can run Sncf
>   (package ncf) on the subsets.
>
>   I cannot find a way to do this with R code.  Getting a distance matrix is
>   easy.  Using that to help generate a series of distance-based subsets is
>   something I cannot figure out (without manually entering a list of all the
>   point pairs).

You mean subsets of _pairs_ of points, right? (If not, then say what you
meant and give an example.)

Do like this:

> cv <- rnorm(20) # phony coords
> dim(cv) <- c(10,2)
> alldist3 <- as.matrix( dist( cv ) ) # Euclidean
> cd <- cut(alldist3,c( 0, 1, 2, 4, Inf) )
> sapply(levels(cd), function(x) which(array(cd == x,dim(alldist3)),arr.ind=TRUE))
\$`(0,1]`
row col
[1,]   2   1
[2,]   5   1
[3,]   6   1
[4,]   9   1
[5,]   1   2
[6,]   3   2
[rest omitted]

Each element of the list contains all the pairs at the specified distance.

You'll want to clean this up to use the right distance and cutpoints and
to rid redundant pairs (2,1 vs 1,2), but you get the idea.

See

?which
?cut
?levels
?sapply

HTH,

Chuck
>
>   Any suggestions would be appreciated.
>   For what it's worth, I did post this question to R-sig-geo as well.
>   Dave Warner
>   David Warner
>   Research Fishery Biologist
>   USGS Great Lakes Science Center
>   Ann Arbor MI 48105
>   734.214.9392
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help