这个怎么样:
# some random data
set.seed(1)
df <- data.frame(x=runif(10), y=runif(10))
# e.g. select obs that have >= 1 neighbour closer than .3 (euclidean)
mat <- as.matrix(dist(df))
sel <- rowSums(mat < .3) >= 2
plot(y~x, df, col = sel + 1L) # viz
# e.g. select obs that have >= 2 neighbours closer than 40000 (great circle/lon,lat)
library(geosphere)
mat <- distm(as.matrix(df))
sel <- rowSums(mat < 40000) >= 3
plot(y~x, df, col = sel + 1L) # viz
# Take 2 random obs from those who meet the criteria
df[sample(which(sel), size = 2), ]
好的,计算约 31000 个数据点之间的距离矩阵可能会使普通计算机窒息。另一种方法可能是使用基于密度的聚类,如 DBSCAN。它可能看起来像这样:
# load your data
set.seed(1)
download.file("https://dl.dropboxusercontent.com/u/17339799/MHI_BF_Survey_Domain_PSU.txt", tf <- tempfile(fileext = ".csv"))
fullds <- read.csv(tf)
df <- fullds[, c("lon_deg", "lat_deg")]
library(dbscan)
kNNdistplot(as.matrix(df), k=4) # determine eps value...
res <- dbscan(as.matrix(df), eps = .005, minPts = 4, borderPoints=F)
# DBSCAN clustering for 31083 objects.
# Parameters: eps = 0.005, minPts = 4
# The clustering contains 134 cluster(s).
# Available fields: cluster, eps, minPts
noise <- res$cluster == 0
sum(noise)
# [1] 2499
# interactive plot with zoom
# (draw rectangle with right mouse,
# CTRL to reset)
library(iplot)
iplot(df$lon_deg, df$lat_deg, col=noise + 1L)
您可能需要对其进行调整以满足您的需求。但是
idx <- sample(which(!noise), 250)
fullds[idx, ]
然后会给你样品。