Need help here. I am trying to create a new column that will list the number of restaurants with in 200 meters of a restaurant using latitude and longitude. I couldn\'t find any
Base R and untested code but you should get the idea.
I'm basically testing how many rows fall within the circle equation x2 + y2 <= R
for each restaurant, except for that restaurant itself, and updating that as the value in the column. Note that the radius in my equation is 200 but it will be different because your x,y is in latitude, longitude and you will have to scale the radius of 200 metres to 2pi radians / circumference of earth
or 360 degree / circumference of earth
.
df <- data.frame(
latitude = runif(n=10,min=0,max=1000),
longitude = runif(n=10,min=0,max=1000)
)
for (i in seq(nrow(df)))
{
# circle's centre
xcentre <- df[i,'latitude']
ycentre <- df[i,'longitude']
# checking how many restaurants lie within 200 m of the above centre, noofcloserest column will contain this value
df[i,'noofcloserest'] <- sum(
(df[,'latitude'] - xcentre)^2 +
(df[,'longitude'] - ycentre)^2
<= 200^2
) - 1
# logging part for deeper analysis
cat(i,': ')
# this prints the true/false vector for which row is within the radius, and which row isn't
cat((df[,'latitude'] - xcentre)^2 +
(df[,'longitude'] - ycentre)^2
<= 200^2)
cat('\n')
}
Output -
1 : TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE
2 : FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
3 : FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
4 : TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE
5 : FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
6 : TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE
7 : FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
8 : FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
9 : FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
10 : FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
> df
latitude longitude noofcloserest
1 189.38878 270.25004 2
2 402.36853 879.26657 0
3 747.46417 581.66627 1
4 291.64303 157.75450 2
5 830.10699 736.19586 2
6 299.06803 157.76147 2
7 725.68360 58.53049 1
8 893.31904 772.46217 1
9 45.47875 701.82201 0
10 645.44772 226.95042 1
What that output means is that for the coordinates at row 1, three rows are within 200 m. Row 1 itself, and rows 4 and 6.