Skip to content

Instantly share code, notes, and snippets.

@kmader
Created April 3, 2014 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmader/9949532 to your computer and use it in GitHub Desktop.
Save kmader/9949532 to your computer and use it in GitHub Desktop.
Read in data and find nearest neighbor in R
require("ggplot2") # for plots
require("plyr") # for processing dataframes
# the function to find nearest neighbors
# requires data with x, y, and val columns (val is a unique identifier)
find.nn<-function(in.df) {
ddply(in.df,.(val),function(c.group) {
cur.val<-c.group$val[1]
cur.x<-c.group$x[1]
cur.y<-c.group$y[1]
all.butme<-subset(in.df,val!=cur.val)
all.butme$dist<-with(all.butme,sqrt((x-cur.x)^2+(y-cur.y)^2))
min.ele<-subset(all.butme,dist<=min(all.butme$dist))
min.ele.order<-order(runif(nrow(min.ele)))
min.ele<-min.ele[min.ele.order,]
out.df<-data.frame(x=cur.x,y=cur.y,
xe=min.ele$x[1],ye=min.ele$y[1],vale=min.ele$val[1],dist=min.ele$dist[1])
out.df
})
}
in.table<-read.csv("sample-a.csv")
# add a val column
in.table<-cbind(in.table,val=1:nrow(in.table))
# calculate the nearest neighbor distance
in.table.nn<-find.nn(in.table)
# plot each point and color it by how far away the nearest neighbor is
ggplot(in.table.nn,aes(x=x,y=y,color=dist))+geom_point()+coord_equal()+labs(title="Points and NN Distance",color="NN Distance")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment