I have a data frame containing thousands of rows about houses. For each row, there are two columns with the longitude and the latitude of this geographical point.
My aim is to calculate the distance of each point to the same fixed point (e.g. distance to city center) to eventually add the column to my data frame.
I tried with the package geosphere, but I am unable to automatize the computation for each row.
citycenter being the vector with the longitude and latitude of the fixed point, I'm looking for something that should look like dist(citycenter,c(df$longitude,df$latitude))
Because the second vector is a list, R does not consider it as length 2, which is required to compute the distance.
Thanks in advance for your help
c(df$longitude,df$latitude)
is just one long vector.
In this case you need to pass the columns of the data frame
library(geosphere)
distGeo((citycenter, df[, c("longitude", "latitude")])
Now the dist() function will compute the distance between the city center location with every row in the data frame.