ggmap icon indicating copy to clipboard operation
ggmap copied to clipboard

mapdist() returns too many lines with duplicated from/to

Open avsmith opened this issue 9 years ago • 1 comments

When passing a vector of to/from to mapdist that includes repeated pairs of to/from, the returned data frame re-duplicates repeated entries, and returns too many lines.

Here is code that demonstrates the problem.

library(ggmap)
library(dplyr)

# Create data frame with cities for map test, including repeated lines
cities <-c("Paris, France","London,England","Berlin, Germany")
df <- data_frame(from=rep(cities,2)) %>% mutate(to=lead(from,1)) %>% na.omit()

# Limit to the round trip of cities
roundTrip <- df[1:length(cities),]

# Repeat the first leg
tripRepeatedLeg <- df[1:(length(cities)+1),]

# The starting round trip trip gives the same number of results
roundTripDist <- suppressMessages(mapdist(roundTrip$from,roundTrip$to))
nrow(roundTrip)==nrow(roundTripDist)

# Trips with repeated legs return frames with additional rows
tripRepeatedLegDist <- suppressMessages(mapdist(tripRepeatedLeg$from,tripRepeatedLeg$to))
nrow(tripRepeatedLeg)==nrow(tripRepeatedLegDist)

avsmith avatar Oct 01 '15 01:10 avsmith

I just encountered this issue myself. I know this is an old issue, but it still seems to be present. I am getting additional rows. Remove duplicate lines from the resultant dataframe is not an option, as I do have cases where rows can have the same From/To values. For example, I have two rows with the same From/To, but am getting 8 rows back. Two of the 8 are legitimate. The other 6 are duplicative. The OPs reproducible example still demonstrates the problem as well.

drcanak avatar Jun 08 '22 21:06 drcanak