0

I have two dataframes: x and y columns are the coordinates of the respectives households and hospitals. My expected result is obtain two new columns in the household dataframe. One column must show which is the nearest hospital of each household the other one must illustrate the euclidean distance of the nearest hospital of each household.

household<-c("a","b","c","d")
x_household<-c(2,2.1,2.05,1.99)
y_household<-c(20,20.1,19.9,20.2)

hh<-data.frame(household,x_household,y_household)
hh

hospital<-c("f","j")

x_hospital<-c(2.2,2.4)
y_hospital<-c(19,21)

hospital<-data.frame(hospital,x_hospital,y_hospital)
hospital
Amc
  • 131
  • 8
  • You can use the linked dupe to calculate a distance matrix between the houses and hospitals, and then locate the closest with `which.min` and the distance with `min` (applied to each row or column as needed) – Gregor Thomas Jun 12 '23 at 16:26

0 Answers0