Search code examples
rarcmapspatstat

Identify the largest point within the radius of another point?


Use this example data to see what I mean

tag <- as.character(c(1,2,3,4,5,6,7,8,9,10))

species <- c("A","A","A","A","B","B","B","C","C","D")

size <- c(0.10,0.20,0.25,0.30,0.30,0.15,0.15,0.20,0.15,0.15)

radius <- (size*40)

x <- c(9,4,25,14,28,19,9,22,10,2)

y <- c(36,7,15,16,22,24,39,20,34,9)

data <- data.frame(tag, species, size, radius, x, y)


# Plot the points using qplot (from package tidyverse)
qplot(x, y, data = data) +
  geom_point(aes(colour = species, size = size))

Now that you can see the plot, what I want to do is for each individual “species A” point, I’d like to identify the largest point within a radius of size*40.

For example, in the bottom left of the plot you can see that species A (tag 2) would produce a radius large enough to contain the close species D point.

However, the species A point on the far right-hand-side of the plot (tag 3) would produce a radius large enough to contain both of the close species B and species C points, in which case I’d want some sort of output that identifies the largest individual within the species A radius.

I’d like to know what I can run (if anything) on this data set to get find the largest “within radius” point for each species A point and get an output like this:

Species A point ---- Largest point within radius

Species A tag 1 ----- Species C tag 9

Species A tag 2 ----- Species D tag 10

Species A tag 3 ----- Species B tag 5

Species A tag 4 ----- Species C tag 8

I've used spatstat and CTFSpackage to make some plots in the past but I can't figure out how to "find largest neighbor within radius". Perhaps I can tackle this in ArcMAP? Also, this is just a small example dataset. Realistically I will be wanting to find the "largest neighbor within radius" for thousands of points.

Any help or feedback would be greatly appreciated.


Solution

  • Following finds the largest species and tag pair that is within given radius for each of the species.

    all_df <- data # don't wanna have a variable called data
    res_df <- data.frame()
    for (j in 1 : nrow(all_df)) {
    
      # subset the data
      df <- subset(all_df, species != species[j])
      # index of animals within radius
      ind <- which ((df$x - x[j])^2 +  (df$y - y[j])^2 < radius[j]^2 )
    
      # find the max `size` in the subset df
      max_size <- max(df$size[ind])
      # all indices with max_size in df
      max_inds <- which(df$size[ind] == max_size)
      # pick the last one is there is more than on max_size  
      new_ind <- ind[max_inds[length(max_inds)]]
    
      # results in data.frame
      res_df <- rbind(res_df, data.frame(org_sp = all_df$species[j], 
                                         org_tag = all_df$tag[j], 
                                         res_sp = df$species[new_ind], 
                                         res_tag = df$tag[new_ind]))
    }
    
    res_df
    #      org_sp org_tag res_sp res_tag
    # 1       A       1      C       9
    # 2       A       2      D      10
    # 3       A       3      B       5
    # 4       A       4      C       8
    # 5       B       5      A       3
    # 6       B       6      C       8
    # 7       B       7      C       9
    # 8       C       8      B       5
    # 9       C       9      B       7
    # 10      D      10      A       2