I am using R and the provided function called kNNdistplot from the dbscan package to calculate the average distance for each point to k-nearest nei
R
kNNdistplot