我在data.frame中有5个点的名称(列“from”)和它们的坐标(列“cx”,“cy”)。 我想计算距离点的距离" i"到了" j",然后保存最接近当前点的点名称(列"到")。我的数据和代码如下。
> df
from cx cy color category
1 path3389 125.71429 366.64792 #000000 2
2 path3391 220.00000 529.50507 #000000 2
3 path3393 437.14285 632.36218 #000000 2
4 path3395 534.28571 472.36221 #000000 2
5 path3397 602.85712 243.79079 #000080 3
6 path3399 297.14285 69.50506 #008000 1
很抱歉我的数据显示如此。 代码如下:
# sort by 5th colonm = category,
# where 1 - start point, 2 - middile, and 3 - finish point
df <- df[ order(df[,5]), ]
n <- 5
for (i in 1 : n){
rr <- 1000000000
#----------------
for (j in i+1 : n){#nrow(df)) {
sqrt = ((df$cx[i] - df$cx[j])^2 +
(df$cy[i] - df$cy[j])^2 )^(1/2);
if (sqrt < rr) {
rr <- sqrt
tmp <- j
print(rr); print(j)
}
}# forj
df$to[i] <- df$from[tmp] # error is here?
} # fori
结果是
[1] 343.0476
[1] 2
[1] 188.1814
[1] 3
[1] 240.2719
[1] 4
[1] 187.181
[1] 5
[1] 238.6356
[1] 6
这里 343.0476 是1st(path3399)和2nd(path3389)点之间的最小距离,即我应该从列&#34;中取出元素( path3389 ) #34;并写入&#34;到&#34;列的第一行。 这里 188.1814 是第二个(path3389)和第三个(path3391)点之间的最小距离,即我应该从&#34;&#34;中取出元素( path3391 )。并写入&#34;列的第二行&#34;。
预期结果是
> df
from cx cy category to
6 path3399 297.14285 69.50506 1 path3389
1 path3389 125.71429 366.64792 2 path3391
2 path3391 220.00000 529.50507 2 path3393
3 path3393 437.14285 632.36218 2 path3395
4 path3395 534.28571 472.36221 2 path3397
5 path3397 602.85712 243.79079 3 # empty
但现在我明白了:
from cx cy color category to
6 path3399 297.14285 69.50506 #008000 1 1
1 path3389 125.71429 366.64792 #000000 2 2
2 path3391 220.00000 529.50507 #000000 2 3
3 path3393 437.14285 632.36218 #000000 2 4
4 path3395 534.28571 472.36221 #000000 2 5
5 path3397 602.85712 243.79079 #000080 3 1
有人可以告诉我如何编写命令df$to[i] <- df$from[tmp]
在代码中?我检查了df的第一列:
> df$from[tmp]
[1] path3397
5 Levels: path3389 path3391 path3393 path3395 path3397
> df$from[1]
[1] path3399
5 Levels: path3389 path3391 path3393 path3395 path3397
感谢。