Nearest Neighbour in R

Package in use: "spatstat"

Data: 

> title <- c("Name", "X", "Y", "Surface", "Date", "Temprature")
> GWT <- read.csv2("E:\\Slides\\Geostatistics & Geomarketing(Nein)\\data\\Groundwater_Temperature.csv", header = TRUE, sep = ",", dec = ".", col.names = title)
> GWT_sub <- subset(GWT, subset = (GWT$Surface >0))
> head(GWT_sub)
     Name       X       Y Surface    Date Temprature
1 ABP9922 4459725 5340978  497.50 7/25/09        9.9
2 ABP9926 4460005 5341246  498.90 7/25/09       12.0
3   BP 25 4464282 5331784  534.17 7/26/09        9.9
4   BP 26 4464222 5330721  542.08 7/27/09       10.5
5   BP 32 4460921 5337893  509.09 7/25/09       11.6
6   BP 49 4463843 5339414  501.50 7/24/09       11.3


Creat a point pattern dataset with function ppp
> Xgwt_subset <- ppp(GWT_sub$X, GWT_sub$Y, window = owin(c(min(GWT_sub$X), max(GWT_sub$X)), c(min(GWT_sub$Y), max(GWT_sub$Y))), marks = GWT_sub$Temperature)
> plot(Xgwt_subset)
> plot(Xgwt_subset, main = "GWT")


Calculating neighbors of each points. M means that, the 85th point is neighbor of the 1st point, 170th point is the nearest neighbor of 2ed point.  b is another dataset for every points' neighbors.

> m <- nnwhich(GWT_sub$X, GWT_sub$Y)
> m
  [1]   2   1 407 411 257 143 319  14  13  80  12  11   9   8  44  53 107 104 329   6
 [21] 102 268 198 194 284 233 233 170 142  19 299 278  47 159  25 291 131  39  40  39
... ...
> b <- GWT_sub[m, ]

Displaying.

> arrows(GWT_sub$X, GWT_sub$Y, b$X, b$Y, col = "red", length = 0.06)





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值