我正在使用
xing_points <- crossing.psp(a = x, b = x, details = TRUE) %>% as.data.frame()
来自spatstat
包的获取x
数据框中包含的段之间的所有交叉点以及每个交叉点的来源详细信息。
但是,由于我对a
和b
输入使用相同的数据帧,因此每个交叉点在生成的xing_points
数据帧中出现两次。因此,我似乎很自然地使用以下形式的更智能的selfcrossing.psp()
:
xing_points <- selfcrossing.psp(x) %>% as.data.frame()
解决了上述问题,但事实上,缺少details
选项,因此不允许跟踪每个交叉点返回原始交叉段。
如何实现selfcrossing.psp()
函数给出的重复排除和同时details
的指示?
非常感谢。
答案 0 :(得分:1)
在这种情况下,我认为使用crossing.psp
最简单,然后删除重复的行。为了找到重复的行,我使用了所有列都是数字的事实,因此可以对所有行进行排序并以这种方式查找重复的行:
library(spatstat)
set.seed(123)
x <- psp(runif(10), runif(10), runif(10), runif(10), window=owin())
xing_points <- as.data.frame(crossing.psp(A = x, B = x, details = TRUE))
xing_points <- xing_points[!duplicated(t(apply(xing_points, 1, sort))),]
答案 1 :(得分:1)
我将此作为功能请求。在spatstat
的未来版本中,函数selfcrossing.psp
将具有details
参数。 [这需要一些新的C代码。]请查看http://github.com/spatstat/spatstat/releases了解更新。
答案 2 :(得分:0)
Thanks to @ege-rubak I came up with a working solution. I could not comment his answer because my response would be too long, so I decided to write an answer to my own question.
First, I noticed that Ege's solution did not work properly because, due to the algorithm in crossing.psp
, points of interscetion that have the same x
and y
coordinate come from the same couple of instersecting segments but in reverse order. That is, the output of
xing_points <- as.data.frame(crossing.psp(A = x, B = x, details = TRUE))
will contain, for example, both iA = 10
, jB = 6
, and iA = 6
and jB = 10
, making the two rows not duplicated.
The solutions I have found is a little more complicated than expected but it worked just fine. It follows:
library(spatstat)
library(dplyr)
set.seed(123)
xing_points <- psp(df_select$x_t, df_select$y_t, df_select$x_r, df_select$y_r, owin(), check = F) %>%
crossing.psp(., ., details = T) %>%
as.data.frame() %>%
mutate(
iAjB = iA*jB
) %>%
arrange(x, y, iAjB) %>%
group_by(iAjB) %>%
filter(row_number() == 1) %>%
as.data.frame()
I created the iAjB
column, then arranged by x
,y
and iAjB
, grouped by the last iAjB
so that each group contains only the two identical crossing points, and than selected the first point in the group.
Everything seems to work fine now.