0

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

Collin
  • 1
  • Welcome to Stack Overflow! You will find that you get better answers if you take some time to make your question reproducible. Please follow [the guidelines here](http://stackoverflow.com/a/5963610/1036500). Thanks! – Ben Mar 22 '16 at 22:31
  • Thanks for the feedback Ben. In the future I will make the question more friendly for those that are trying to answer it. – Collin Mar 23 '16 at 16:13

1 Answers1

0

I was making this way more complicated than it needed to be... cbind Plants_dist (gDistance output) with Sites.rg@data (shapefile attribute table). Then subset based on values (distances) in the joined columns.

Collin
  • 1