# closepairs

##### Close Pairs of Points

Low-level functions to find all close pairs of points.

##### Usage

`closepaircounts(X, r)`crosspaircounts(X, Y, r)

closepairs(X, rmax, ...)

## S3 method for class 'ppp':
closepairs(X, rmax, ordered=TRUE,
what=c("all","indices","ijd"), ...)

crosspairs(X, Y, rmax, ...)

## S3 method for class 'ppp':
crosspairs(X, Y, rmax, what=c("all", "indices", "ijd"), ...)

##### Arguments

- X,Y
- Point patterns (objects of class
`"ppp"`

). - r,rmax
- Maximum distance between pairs of points to be counted as close pairs.
- ordered
- Logical value indicating whether all ordered pairs of close points
should be returned. If
`ordered=TRUE`

, each pair will appear twice in the output, as`(i,j)`

and again as`(j,i)`

. If`ordered=FALSE`

- what
- String specifying the data to be returned for each close pair of points.
If
`what="all"`

(the default) then the returned information includes the indices`i,j`

of each pair, their`x,y`

coordinates, and the dis - ...
- Extra arguments, ignored by methods.

##### Details

These are the efficient low-level functions used by

`closepaircounts(X,r)`

counts the number of neighbours for
each point in the pattern `X`

. That is, for each point
`X[i]`

, it counts the number of other points `X[j]`

with `j != i`

such that ```
d(X[i],X[j]) <= r<="" code=""> where
```

`d`

denotes Euclidean distance. The result is an integer vector
`v`

such that `v[i]`

is the number of neighbours of
`X[i]`

.

`crosspaircounts(X,Y,r)`

counts, for each point
in the pattern `X`

, the number of neighbours in the pattern
`Y`

. That is, for each point
`X[i]`

, it counts the number of points `Y[j]`

such that ```
d(X[i],X[j]) <= r<="" code="">. The result is an integer vector
```

`v`

such that `v[i]`

is the number of neighbours of
`X[i]`

in the pattern `Y`

.

`closepairs(X,rmax)`

identifies all pairs of neighbours
in the pattern `X`

and returns them. The result is
a list with the following components:
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
If `what="indices"`

then only the components `i`

and
`j`

are returned. This is slightly faster and more efficient
with use of memory.

`crosspairs(X,rmax)`

identifies all pairs of neighbours
`(X[i], Y[j])`

between the patterns `X`

and `Y`

,
and returns them. The result is
a list with the same format as for `closepairs`

.

##### Value

- For
`closepaircounts`

and`crosspaircounts`

, an integer vector of length equal to the number of points in`X`

.For

`closepairs`

and`crosspairs`

, a list with components`i`

and`j`

, and possibly other components as described under Details.

##### Warning about accuracy

The results of these functions may not agree exactly with
the correct answer (as calculated by a human) and may not
be consistent between different computers and different installations
of R. The discrepancies arise in marginal cases where the interpoint
distance is equal to, or very close to, the threshold `rmax`

.

Floating-point numbers in a computer
are not mathematical Real Numbers: they are approximations using
finite-precision binary arithmetic.
The approximation is accurate to a tolerance of about
`.Machine$double.eps`

.

If the true interpoint distance $d$ and the threshold `rmax`

are equal, or if their difference is no more than `.Machine$double.eps`

,
the result may be incorrect.

##### See Also

`closepairs.pp3`

for the corresponding
functions for 3D point patterns.
`Kest`

, `Kcross`

,
`nndist`

, `nncross`

,
`applynbd`

, `markstat`

for functions which use these capabilities.

##### Examples

```
a <- closepaircounts(cells, 0.1)
sum(a)
Y <- split(amacrine)
b <- crosspaircounts(Y$on, Y$off, 0.1)
d <- closepairs(cells, 0.1)
e <- crosspairs(Y$on, Y$off, 0.1)
```

*Documentation reproduced from package spatstat, version 1.42-2, License: GPL (>= 2)*