使用此示例数据来查看我的意思
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中解决这个问题?此外,这只是一个小示例数据集。实际上,我会想要找到数千个点的“半径范围内最大的邻居”。
非常感谢任何帮助或反馈。
答案 0 :(得分:0)
以下找到每个物种在给定半径范围内的最大物种和标签对。
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