find point pairs with equal spatial coordinates

zerodist(obj, zero = 0.0, unique.ID = FALSE, memcmp = TRUE)
zerodist2(obj1, obj2, zero = 0.0, memcmp = TRUE)
remove.duplicates(obj, zero = 0.0, remove.second = TRUE, memcmp = TRUE)

Arguments

obj

object of, or extending, class SpatialPoints

obj1

object of, or extending, class SpatialPoints

obj2

object of, or extending, class SpatialPoints

zero

distance values less than or equal to this threshold value are considered to have zero distance (default 0.0); units are those of the coordinates for projected data or unknown projection, or km if coordinates are defined to be longitude/latitude

unique.ID

logical; if TRUE, return an ID (integer) for each point that is different only when two points do not share the same location

memcmp

use memcmp to find exactly equal coordinates; see NOTE

remove.second

logical; if TRUE, the second of each pair of duplicate points is removed, if FALSE remove the first

Value

zerodist and zerodist2 return a two-column matrix with in each row pairs of row numbers with identical coordinates; a matrix with zero rows is returned if no such pairs are found. For zerodist, row number pairs refer to row pairs in obj. For zerodist2, row number pairs refer to rows in obj and obj2, respectively. remove.duplicates removes duplicate observations if present, and else returns obj.

Note

When using kriging, duplicate observations sharing identical spatial locations result in singular covariance matrices. This function may help identify and remove spatial duplices. The full matrix with all pair-wise distances is not stored; the double loop is done at the C level.

When unique.ID=TRUE is used, an integer index is returned. sp 1.0-14 returned the highest index, sp 1.0-15 and later return the lowest index.

When zero is 0.0 and memcmp is not FALSE, zerodist uses memcmp to evaluate exact equality of coordinates; there may be cases where this results in a different evaluation compared to doing the double arithmetic of computing distances.

Examples

data(meuse) summary(meuse)
#> x y cadmium copper #> Min. :178605 Min. :329714 Min. : 0.200 Min. : 14.00 #> 1st Qu.:179371 1st Qu.:330762 1st Qu.: 0.800 1st Qu.: 23.00 #> Median :179991 Median :331633 Median : 2.100 Median : 31.00 #> Mean :180005 Mean :331635 Mean : 3.246 Mean : 40.32 #> 3rd Qu.:180630 3rd Qu.:332463 3rd Qu.: 3.850 3rd Qu.: 49.50 #> Max. :181390 Max. :333611 Max. :18.100 Max. :128.00 #> #> lead zinc elev dist #> Min. : 37.0 Min. : 113.0 Min. : 5.180 Min. :0.00000 #> 1st Qu.: 72.5 1st Qu.: 198.0 1st Qu.: 7.546 1st Qu.:0.07569 #> Median :123.0 Median : 326.0 Median : 8.180 Median :0.21184 #> Mean :153.4 Mean : 469.7 Mean : 8.165 Mean :0.24002 #> 3rd Qu.:207.0 3rd Qu.: 674.5 3rd Qu.: 8.955 3rd Qu.:0.36407 #> Max. :654.0 Max. :1839.0 Max. :10.520 Max. :0.88039 #> #> om ffreq soil lime landuse dist.m #> Min. : 1.000 1:84 1:97 0:111 W :50 Min. : 10.0 #> 1st Qu.: 5.300 2:48 2:46 1: 44 Ah :39 1st Qu.: 80.0 #> Median : 6.900 3:23 3:12 Am :22 Median : 270.0 #> Mean : 7.478 Fw :10 Mean : 290.3 #> 3rd Qu.: 9.000 Ab : 8 3rd Qu.: 450.0 #> Max. :17.000 (Other):25 Max. :1000.0 #> NA's :2 NA's : 1
# pick 10 rows n <- 10 ran10 <- sample(nrow(meuse), size = n, replace = TRUE) meusedup <- rbind(meuse, meuse[ran10, ]) coordinates(meusedup) <- c("x", "y") zd <- zerodist(meusedup) sum(abs(zd[1:n,1] - sort(ran10))) # 0!
#> [1] 489
# remove the duplicate rows: meusedup2 <- meusedup[-zd[,2], ] summary(meusedup2)
#> Object of class SpatialPointsDataFrame #> Coordinates: #> min max #> x 178605 181390 #> y 329714 333611 #> Is projected: NA #> proj4string : [NA] #> Number of points: 155 #> Data attributes: #> cadmium copper lead zinc #> Min. : 0.200 Min. : 14.00 Min. : 37.0 Min. : 113.0 #> 1st Qu.: 0.800 1st Qu.: 23.00 1st Qu.: 72.5 1st Qu.: 198.0 #> Median : 2.100 Median : 31.00 Median :123.0 Median : 326.0 #> Mean : 3.246 Mean : 40.32 Mean :153.4 Mean : 469.7 #> 3rd Qu.: 3.850 3rd Qu.: 49.50 3rd Qu.:207.0 3rd Qu.: 674.5 #> Max. :18.100 Max. :128.00 Max. :654.0 Max. :1839.0 #> #> elev dist om ffreq soil lime #> Min. : 5.180 Min. :0.00000 Min. : 1.000 1:84 1:97 0:111 #> 1st Qu.: 7.546 1st Qu.:0.07569 1st Qu.: 5.300 2:48 2:46 1: 44 #> Median : 8.180 Median :0.21184 Median : 6.900 3:23 3:12 #> Mean : 8.165 Mean :0.24002 Mean : 7.478 #> 3rd Qu.: 8.955 3rd Qu.:0.36407 3rd Qu.: 9.000 #> Max. :10.520 Max. :0.88039 Max. :17.000 #> NA's :2 #> landuse dist.m #> W :50 Min. : 10.0 #> Ah :39 1st Qu.: 80.0 #> Am :22 Median : 270.0 #> Fw :10 Mean : 290.3 #> Ab : 8 3rd Qu.: 450.0 #> (Other):25 Max. :1000.0 #> NA's : 1
meusedup3 <- subset(meusedup, !(1:nrow(meusedup) %in% zd[,2])) summary(meusedup3)
#> Object of class SpatialPointsDataFrame #> Coordinates: #> min max #> x 178605 181390 #> y 329714 333611 #> Is projected: NA #> proj4string : [NA] #> Number of points: 155 #> Data attributes: #> cadmium copper lead zinc #> Min. : 0.200 Min. : 14.00 Min. : 37.0 Min. : 113.0 #> 1st Qu.: 0.800 1st Qu.: 23.00 1st Qu.: 72.5 1st Qu.: 198.0 #> Median : 2.100 Median : 31.00 Median :123.0 Median : 326.0 #> Mean : 3.246 Mean : 40.32 Mean :153.4 Mean : 469.7 #> 3rd Qu.: 3.850 3rd Qu.: 49.50 3rd Qu.:207.0 3rd Qu.: 674.5 #> Max. :18.100 Max. :128.00 Max. :654.0 Max. :1839.0 #> #> elev dist om ffreq soil lime #> Min. : 5.180 Min. :0.00000 Min. : 1.000 1:84 1:97 0:111 #> 1st Qu.: 7.546 1st Qu.:0.07569 1st Qu.: 5.300 2:48 2:46 1: 44 #> Median : 8.180 Median :0.21184 Median : 6.900 3:23 3:12 #> Mean : 8.165 Mean :0.24002 Mean : 7.478 #> 3rd Qu.: 8.955 3rd Qu.:0.36407 3rd Qu.: 9.000 #> Max. :10.520 Max. :0.88039 Max. :17.000 #> NA's :2 #> landuse dist.m #> W :50 Min. : 10.0 #> Ah :39 1st Qu.: 80.0 #> Am :22 Median : 270.0 #> Fw :10 Mean : 290.3 #> Ab : 8 3rd Qu.: 450.0 #> (Other):25 Max. :1000.0 #> NA's : 1
coordinates(meuse) <- c("x", "y") zerodist2(meuse, meuse[c(10:33,1,10),])
#> [,1] [,2] #> [1,] 10 1 #> [2,] 11 2 #> [3,] 12 3 #> [4,] 13 4 #> [5,] 14 5 #> [6,] 15 6 #> [7,] 16 7 #> [8,] 17 8 #> [9,] 18 9 #> [10,] 19 10 #> [11,] 20 11 #> [12,] 21 12 #> [13,] 22 13 #> [14,] 23 14 #> [15,] 24 15 #> [16,] 25 16 #> [17,] 26 17 #> [18,] 27 18 #> [19,] 28 19 #> [20,] 29 20 #> [21,] 30 21 #> [22,] 31 22 #> [23,] 32 23 #> [24,] 33 24 #> [25,] 1 25 #> [26,] 10 26