Hello,
I have a data frame containing geocoded latitude and longitude columns. These values are missing in some rows and I want to supplement them with corresponding values from other rows containing the same address.
What I want to do is basically this: if lat & long are missing, supplement it with values from other nearest row having the same id and var1 columns.
To be concrete: in my dummy DF below, I want to supplement lat and long in rows 1 and 7 with values from row 4; and missing values in row 5 with values from row 2.
Lat and long in row 6 remains empty because we have no corresponding rows with lat and long columns.
How to do this in tidyverse way please?
Many thanks in advance
df <- tribble(
~id, ~var1, ~lat, ~long,
100, "G", "", "",
250, "O", "24.56", "21.06",
300, "O", "31.55", "25.64",
100, "G", "24.50", "26.88",
250, "O", "", "",
800, "K", "", "",
100, "G", "", ""
)
# A tibble: 7 × 4
id var1 lat long
<dbl> <chr> <chr> <chr>
1 100 G "" ""
2 250 O "24.56" "21.06"
3 300 O "31.55" "25.64"
4 100 G "24.50" "26.88"
5 250 O "" ""
6 800 K "" ""
7 100 G "" ""