I want to loop some code that checks the latitude and longitude of one row of a df1 against all the rows of latitude and longitude of df2 for their distance, and then filters for those distances less than 500. I want to then drop down to the next row of df1 and do the same task.
I can achieve it for the first row, but I haven't been successful in looping it.
I have tried wrapping it in a loop function:
for (i in nrow(df3$value)){
df <- df2 %>%
filter(distHaversine(c(Longitude, Latitude), c(df3$Longitude[i], df3$Latitude[i])) <= 500)
df <- data.frame(df, df3$value[i])
}
but that hasn't been successful.
This code below returns when copied into the console returns the correct information against 1 row of df3. I want to be able to run it against all 75 rows of df3.
df <- df2 %>%
filter(distHaversine(c(Longitude, Latitude), c(df3$Longitude[1], df3$Latitude[1])) <= 500)
df <- data.frame(df, df3$value[1])