R - Outlier Detection Algorithm
In my opinion there are only few aspects:


Consistent use of assignment operator (either = or &lt;-, mind that in special cases they aren't equivalent)
I like that you use package-references (::), you forgot about RANN:: though
You could use the purrr-package to replace sapply-type commands, which gives you more control over the output
When you refer to columns in a fixed position way such as colnames(data)[1] you should make sure that the data structure you give to the function is as expected