I am trying to join the distances from gDistance output to one of the shapefiles I used to generate the distance matrix and I cannot figure it out. I thought I had it but realized that by joining on row name, the distances were being joined to the wrong records. Here is what I have done so far.
Plants_dist <- gDistance(Sites.rg, Plants.rg, byid=T, hausdorff=F, densifyFrac = NULL)
Plants_dist.df <- as.data.frame(Plants_dist, row.names = TRUE)
I am interested in only records inside of 16093.44 meters...
Plants_dist.df[apply(Plants_dist.df, MARGIN = 1, function(x) any(x < 16093.44)),]
This is where it breaks down... when I tried to merge the records on row.names to create an output table of the Plants with distances to Sites I get the proper distance value define above but it merges the distance to the wrong records.
Dist_Site_Plants_list <- merge(Plants.rg@data, Sites_Plants_list, by.x="row.names", by.y="row.names", all.x=F, sort=F)
I think this is because of the row names are not sorted properly but I cannot figure out how else to do this