How to extract distances from delaunay triangulation to list object in R -
Suppose I have coordinates of digits, with each ID, I can put my data into this structure id & lt; -c ("A", "B", "C", "C".)
"D", "E", "F", "G" , "H", "I", "J", "K", "L", "M", "N") x_coor & lt; -c 0.5,1,1,1,5,2,3,3,3,5,4,4,5,5,5,6,7,7) y_coor & lt; - c (5.5,3,7,6,5,5,3,5,3,1,5, 1,2,5,4,5,3,5,5.5) my.data & lt; - data.frame (id = id, x_coor = x_coor, y_coor = y_coor) # When I perform deluxe triangular, then I can see the distance .... Library (tripeak) my.triangles & lt; -ri.mesh (my.data $ x_coor, my.data $ y_coor) Plot (my.triangles, do.points = FALSE, lwd = 0.2) digits (my.data $ x, my .data $ y, col = " Black ", pch = 20, cex = 1.5) text (my.data $ x, my.data $ y, labels = my.data $ id)
How can I remove" pairs "of the list points in this way the object?
# I want something like this ... my.list [[A]] [1] 2.55 1.58 1.41 1.58 (all the distance connected to "A") [[B]] [1 ] 2.55 2.24 2.06 2.00 2.92 3.61 (All the distance connected to "B") etc.
< P> tri.mesh () We have: my_triangles & lt; - str.mesh (my.data $ x_coor, my.data $ y_coor) from the plot (my_triangles, do digits = FALSE, lwd = 0.2) to str (my_triangles) < / Code> and R documents of neighbors We can move forward by extracting the neighbors for each point as follows: neiblist & lt; - Neighbors (my_triangles) and then add the point ID from the original dataframe so you almost have that list, which includes the neighboring ID and does not have the distance:
name (neiblist) & lt; - name my.data $ id # for later reference Then calculate an Euclidean distance matrix with all the points:
Euc_dist & lt; - as.matrix (dist (cbind (x = my_triangles $ x, y = my_triangles $ y))) Dimendames (euc_dist) for # reference columns & lt; - My.data $ id r Memory preallax is required. max_n & lt; - max (list (neiblist, length)) npoints & lt; - Length (my.data $ id) # This is the total number of digits Lock the memory to collect the results, Important for computational efficiency and speed : dist_2neigh_mat & lt; - Matrix (Enro = Npins, NXL = Max_N) #Create Results Matrix rownames (dist_2neigh_mat) & lt; - my.data $ id colnames (dist_2neigh_mat) & lt; - Colnames (data.frame (dist = matrix (data = 1: 6, nero = 1))) Obtain and collect distance vectors for all points.
for (in my.data $ id) {neighbors_ i < - neiblist [[i]] dist2neighbours_i & lt; - euc_dist [, I] [neighbors_i] #Append NAS match to vector result matrix dist2neighbours_i & lt ncol; - c (dist2neighbrs_i, rep (na, bar = (max_n-length (dist2neighbours_i))) dist_2neigh_mat [i]] dist_2neigh_mat is your result. If you insist on writing your results properly in the list, then you need to convert the metrics of the results into a list: results_list & lt; - as.list (Data.frame (t (dist_2neigh_mat))) You can get rid of NA generated before matrix integrity reasons:
#A fun NAA of the NA NA & Lt; - Function (x) {x = x [! Is.na (x)] Return (x)} Remove NM from the results
results_list & lt; - lapply (results_list, fun = fun_NA) My thinking is that it will be very fast with many and many points .. but someone may know differently : -)
Cheers.
Comments
Post a Comment