0

My data is similar to the following:

# A tibble: 7 x 3
# Groups:   offense [7]
    lon   lat offense           
  <dbl> <dbl> <fct>             
1 -95.3  29.8 aggravated assault
2 -95.4  29.9 auto theft        
3 -95.3  29.8 burglary          
4 -95.5  29.7 murder            
5 -95.4  30.0 rape              
6 -95.5  29.8 robbery           
7 -95.4  29.8 theft  

I can run the following

cbind(df, X = rowSums(distm(df[,1:2], fun = distHaversine) / 1000 <= 10))

# A tibble: 7 x 4
# Groups:   offense [7]
    lon   lat offense                X
  <dbl> <dbl> <fct>              <dbl>
1 -95.3  29.8 aggravated assault     3
2 -95.4  29.9 auto theft             2
3 -95.3  29.8 burglary               3
4 -95.5  29.7 murder                 1
5 -95.4  30.0 rape                   2
6 -95.5  29.8 robbery                1
7 -95.4  29.8 theft                  3

Which gives me the number of points within a radius of 10km according to this SO post.

What I would like to know is how to modify that function to give me which rows correspond to each point within a radius. The first row has a value of 3, this value might be made up of an observation from rows 2, 4 and 7 for example.

It might look like:

    lon   lat offense                X    points

1 -95.3  29.8 aggravated assault     3   c(2,4,7)
2 -95.4  29.9 auto theft             2   c(2,3)
3 -95.3  29.8 burglary               3   c(4,5,7)

Once I have these I would like to create lists such that row 1 would be a list containing lists of 1, 2, 4 and 7. (However this might be a different quesiton)

Data:

library(geosphere)
library(ggmap)
df <- crime %>% 
  group_by(offense) %>% 
  sample_n(1) %>% 
  select(lon, lat, offense)
user113156
  • 6,023
  • 4
  • 27
  • 60

0 Answers0