# [R] Sectors covered by the Nearest Neighbors

Eliza Botto e||z@_botto @end|ng |rom out|ook@com
Tue Aug 17 03:03:52 CEST 2021

```deaR useRs,

I have a very basic question. I am for putting up a real long text. But I tried searching for some clues in the previous help posts but unfortunately, I could get any. I have the following data called "el".

> dput(el)

structure(c(1.00451374640952, 1.88100123102175, 0.201887566680345,
0.339762002462043, 0.211735740664752, 1.29011079195732, 1.72343044727123,
2.07304062371769, 0.58596635207222, 0.994665572425113, 0.398851046368486,
1.29503487894953, 1.01025376658795, 1.00853759936143, 1.015781421492,
1.01308743805501, 1.00815844613696, 0.994069910533143, 0.993411381248545,
0.987205241627033, 0.98411214953271, 0.987005687298367, 1.01957295373665,
0.978804004390195, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), .Dim = c(12L,
3L))

you see, there are 3 columns. The first 2 columns contain actual data whereas the third column contains the "gate numbers" for which the data is presented in the first 2 columns. I then executed distance matrix by using the data in the first 2 columns by using the following command

> d53<-dist(el[,1])

> d15<-d53/mean(d53)

> d54<-dist(el[,2])

> dr1<-d54/mean(d54)

> t<-as.matrix((d15)^2+(dr1)^2)

> w<-sqrt(t)

I then sorted the nearest neighbors of "gate 6" in the order of their distance from the data of "gate 6"

> u<-matrix(sort(as.matrix(w)[6,],index.return=TRUE)\$ix,ncol=1)

Finally, I plotted the entire data by giving different colors to "gate 6" (red) and its 5 nearest neighbors (green)

> plot(el[,1],el[,2])

> textxy(el[,1],el[,2],el[,3])

> points(el[,1],el[,2],col="red",pch=16)

> points(el[,1][u],el[,2][u],col="green",pch=16)

> points(el[,1][u],el[,2][u],col="green",pch=16)

> points(el[,1][u],el[,2][u],col="green",pch=16)

> points(el[,1][u],el[,2][u],col="green",pch=16)

> points(el[,1][u],el[,2][u],col="green",pch=16)

Afterwards I bisected the space around gate 6 into 6 sectors each covering 60 degrees.

> X1<-(el[,1])

> Y1<-(el[,2])

> X2<-1.5*X1

> Y2<-Y1

> base<-sqrt((X1-X2)^2+(Y1-Y2)^2)

> Hyp =base/cos(60*pi/180)

> Pre= Hyp*sin(60*pi/180)

> Y3<-Pre+Y2

> X3<-X2

> segments((X3), (Y3), (X1-base), (Y1-Pre),type="l",col="red")

> segments((X1-base), (Y3), (X3), (Y1-Pre),type="l",col="red")

> segments((X2), (Y2), (X1-base), (Y1),type="l",col="red")

As you can see from the plot that the nearest neighbours of gate 6 have covered 4 (of those 6) sectors. For example, the gates 7 and 8 are covering the sector (301 to 360 degrees), the sectors (0 to 60 degrees and 61 to 120 degrees) are vacant so they are not counted, the sector (121 to 180 degrees) is covered by gate 1, gate 10 covers the sector from 181 to 240 degrees, and gate 12 covers the sector from 241 to 300 degrees.  Currently, I have to manually write down the number of sectors the nearest neighbours of each gate are covering. Is there a command that can help me executing ONLY the TOTAL number of covered sectors around each gate directly (4 in the current case)?

The gate 6 here is just given as an example. I have almost 3000 gates for which I have to find the number of covered sectors by the nearest neighbours.

I hope there is a way around it. I thank-you all in advance.

Regards,

Eliza

Trinity College Dublin

[[alternative HTML version deleted]]

```