setwd("/home/michael/Dropbox/BGU/1_Other/Rumeng_He/")

dat = read.csv("The location  of tree in plot.csv", stringsAsFactors = FALSE)

# Select tree
i = "p2-5"

# Calculate distances
dat$dist = sqrt(
  (dat$X.m[dat$Code.of.focal.tree == i] - dat$X.m)^2 +
  (dat$Y.m[dat$Code.of.focal.tree == i] - dat$Y.m)^2 
)

# Subset <8m
dat = dat[dat$dist < 8, ]

# Calculate CI
sum(
  (
    dat$DBH.cm[dat$Code.of.focal.tree != i] / 
    dat$DBH.cm[dat$Code.of.focal.tree == i]
  ) / dat$dist[dat$Code.of.focal.tree != i]
)
## [1] 4.902929