In R, I need to calculate the nearest neighbour index between the relocations of a single animal, for several different individuals. I want to determine whether the relocations of certain individuals are more randomly or regularly distributed than those of other animals.
I have a dataset similar to this:
ID<- c(a,a,a,a,a,b,b,b,b,b,c,c,c,c,c,c,d,d,d,d,d, etc) E<-c(167685,167945,167685,153985,167685,158675,167645,167667, etc) N<-c(9876548,9879248,9876838,9596548,9926548,9878578,9876548,9166548, etc)
In which each "ID" represents a different animal, and E/N points represent locations where the animals were observed. The data frame, consisting of 120 observations of 6 individuals with 20 observations each is called "cor".
I know I can use the "nni" function in the "spatialEco" package to obtain the NNI, the problem is I need to sequentially subset the original data frame in order to calculate NNI for each individual in the sample:
library(spatialEco) a<-subset(cor, ID=="a") coordinates(a) <- ~E+N nni(a, win = "hull")
Being this the output:
$NNI  1.942733 $z.score  8.065562 $p  7.289959e-16 $expected.mean.distance  0.1731123 $observed.mean.distance  0.336311
I would like to create a loop of some kind which would do this set of commands for each individual (ID). In the output table, each row would summarize the NNI and the rest of parameters fr each individual, like so:
ID NNI z.score etc a 0.87 2.34 - b 1.45 3.12 - c 2.13 4.05 -