Skip to content

Instantly share code, notes, and snippets.

@FranciscoGutierrez
Created May 16, 2017 16:27
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 FranciscoGutierrez/c405499974faf47935b8b6988bdbef61 to your computer and use it in GitHub Desktop.
Save FranciscoGutierrez/c405499974faf47935b8b6988bdbef61 to your computer and use it in GitHub Desktop.
Compare and merge two locations based on treshold.
foursquare = read.csv("foursquarela.csv")
opendata = read.csv("poisla.csv")
opendata.la = subset(opendata, CITY=='LOS ANGELES')
fs.df <- data.frame(fs.id = foursquare$poi.id, lat = foursquare$latitude, lon = foursquare$longitude)
od.df <- data.frame(od.id = opendata.la$X34.092793, lat = opendata.la$lat2, lon = opendata.la$lon2)
fs.id <- c()
fs.lat <- c()
fs.lon <- c()
od.id <- c()
od.lat <- c()
od.lon <- c()
for(i in 1:nrow(od.df)) {
od.row <- od.df[i,]
od.lat <- as.numeric(od.row[["lat"]])
od.lon <- as.numeric(od.row[["lon"]])
for (j in 1:nrow(fs.df)) {
fs.row <- fs.df[j,]
fs.lat <- as.numeric(fs.row[["lat"]])
fs.lon <- as.numeric(fs.row[["lon"]])
distance <- ((od.lat - fs.lat)^2 + (od.lon - fs.lon)^2)
if(distance < 0.000001) {
fs.id <- c(fs.id , fs.row[["fs.id"]])
fs.lat <- c(fs.lat, fs.lat)
fs.lon <- c(fs.lon, fs.lon)
od.id <- c(od.id , od.row[["od.id"]])
od.lat <- c(od.lat, od.lat)
od.lon <- c(od.lon, od.lon)
}
}
}
output <- data.frame(fs.id, fs.lat, fs.lon, od.id, od.lat, od.lon)
write.csv(output, file = "output.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment