2017-10-18 76 views
1

使用这个例子中的数据明白我的意思现在确定另一点的半径内的最大点?

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

,你可以看到的情节,就是我想要做的是为每个单独的“物种”点,我想找出最大点在* 40的半径范围内。

例如,在图的左下方,可以看到物种A(标记2)将产生足够大的半径以包含接近物种D点。

然而,图的最右侧(标记3)上的物种A点会产生足够大的半径以包含紧密物种B和物种C点,在这种情况下,我会想要某种类型的输出来识别物种A半径内的最大个体。

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

物种甲点 ---- 最大半径内点

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

物种A标签2 -----种类d标签10

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

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

我用spatstat和CTFSpackage使在过去的一些情节但我无法弄清楚如何“找到半径内最大的邻居”。也许我可以在ArcMAP中解决这个问题?另外,这只是一个小例子数据集。实际上,我会想要找到“数以千计的点内半径最大的邻居”。

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

+0

在'base'可以只计算每个点的欧几里得距离,以各物种A标签作为'SQRT((X1-X2)^ 2 +(Y1-Y2)^ 2)'。一旦你有一个到Species A标签的距离矢量,你可以使用'max(距离_矢量[距离_矢量<40])'。查看是否可以为单个案例进行设置,然后针对每个物种A标记进行迭代。 – Djork

+1

整个个体(或其中心点)是否需要落入该半径? –

+0

随着物种一个标签1,你有B 7也有相同的大小...或者我不是正确的理解? – Suren

回答

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 
+0

非常感谢。 我忘了补充说,如果有两种或两种以上不同的物种在半径范围内相同(如物种A标签1),那么只有最高等级的物种会被选中。所以A会选择B,C和D.而物种B只会选择C和D,依此类推。基本上我想包括一个物种层次结构,将选择范围缩小到一个点。 – Jay

+0

“排名”是什么意思? – Suren

+0

我的脚本出错了 错误(df $ size [ind] == maxs):找不到对象'maxs' – Jay