【问题标题】:Identify the largest point within the radius of another point?确定另一个点半径内的最大点?
【发布时间】:2017-10-18 23:06:22
【问题描述】:

使用此示例数据来了解我的意思

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))

现在你可以看到情节了,我想做的是对于每个单独的“物种 A”点,我想确定半径 *40 内的最大点。

例如,在图的左下角,您可以看到物种 A(标签 2)会产生足够大的半径以包含靠近的物种 D 点。

但是,绘图最右侧的物种 A 点(标签 3)会产生足够大的半径以包含靠近的物种 B 和物种 C 点,在这种情况下,我想要某种输出,可以识别物种 A 半径内的最大个体。

我想知道我可以在这个数据集上运行什么(如果有的话)以找到每个物种 A 点的最大“半径内”点并获得如下输出:

物种A点 ---- 半径内的最大点

物种 A 标签 1 ----- 物种 C 标签 9

物种 A 标记 2 ----- 物种 D 标记 10

物种 A 标签 3 ----- 物种 B 标签 5

物种A标签4 -----物种C标签8

我过去曾使用 spatstat 和 CTFSpackage 制作过一些图,但我不知道如何“找到半径内的最大邻居”。也许我可以在 ArcMAP 中解决这个问题?此外,这只是一个小的示例数据集。实际上,我会想为数千个点找到“半径内最大的邻居”。

任何帮助或反馈将不胜感激。

【问题讨论】:

  • base 中,您可以将每个点到每个物种A 标记的欧几里得距离计算为sqrt((x1-x2)^2 + (y1-y2)^2)。一旦你有了一个到 Species A 标签的距离向量,你就可以使用像max(distances_vector[distances_vector &lt; 40]) 这样的东西。看看你是否可以为单个案例设置它,然后为每个 Species A 标签进行迭代。
  • 整个个体(或只是其中心的点)是否需要落入该半径范围内?
  • 使用物种 A 标签 1,你有同样大小的 B 7……或者我理解不正确?
  • @JoshO'Brien 就在它的中心点。
  • @Headpoint Species A 标签 1 的尺寸为 0.10,Species B 标签 7 的尺寸为 0.15

标签: r arcmap spatstat


【解决方案1】:

以下查找每个物种在给定半径内的最大物种和标签对。

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

【讨论】:

  • 非常感谢。我忘了补充一点,如果 2 个或更多不同的物种大小相同且在半径范围内(例如物种 A 标签 1),则只会选择排名最高的物种。所以 A 会被选择而不是 B、C 和 D。而物种 B 只会被选择而不是 C 和 D,等等。基本上我想包括一个物种层次结构,它将选择范围缩小到一个单独的点。
  • “排名”是什么意思?改变它以获得你所追求的并不难......
  • 我的脚本出现错误 Error in which(df$size[ind] == maxs) : object 'maxs' not found
  • 这些结果正是我想要的,谢谢。我只是在运行它时遇到错误,有什么想法吗?
  • 再试一次。我之前忘记贴一行了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-25
  • 1970-01-01
  • 1970-01-01
  • 2016-07-21
  • 1970-01-01
  • 2011-03-14
相关资源
最近更新 更多