r - 计算二维欧几里得距离并将其作为列添加到数据中
问题描述
我需要创建一个变量来测量与 APP 的距离。APP的中心在坐标1440000东,12160000北。我需要创建一个新列来计算每个人与APP(dist.APP)中心之间的欧几里得距离。
这是我到目前为止所拥有的,但我不确定该等式是否适用于二维:
east = "1440000"
north = "12160000"
b1 = east
b2 = north
dist.APP <- function(a1, b1, a2, b2) {sqrt((a1 - b1)^2 + (a2 -
b2)^2)
apply(datwolfcoy, FUN = dist.APP, MARGIN = 2)
}
这是您为APP坐标中心指定向量的方式吗?
为了将它添加为每个人的列,这不太奏效,我认为它适用于所有列,而不仅仅是我指定的个人(边距 = 2)。
我也试过这个没有运气:
dist.APP <- data.frame(function(a1, b1, a2, b2) {sqrt((a1 - b1)^2 + (a2 - b2)^2)
}
这是我的数据的样子:
ID PackNumber StudyArea CoyoteAncestry(Logit) PrimaryRds SecondaryRds TertiaryRds Deer Moose east north
49-2 1 Out -0.834473518 0.088 0.499 0.015 0.087 0.112 1358690 12086700
49-3 2 Out -2.408854287 0 0.302 0.188 0 0.382 1346840 12099300
49-4 2 Out -3.896934876 0 0.5 0.164 0.057 0.385 1343380 12100000
49-7 2 Out -2.699548556 0 0.5 0.164 0.057 0.385 1343380 12100000
解决方案
一个可能的解决方案:
df <- data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
ID = c("49-2", "49-3", "49-4", "49-7"),
PackNumber = c(1L, 2L, 2L, 2L),
StudyArea = c("Out", "Out", "Out", "Out"),
`CoyoteAncestry(Logit)` = c(-0.834473518,-2.408854287,-3.896934876,
-2.699548556),
PrimaryRds = c(0.088, 0, 0, 0),
SecondaryRds = c(0.499, 0.302, 0.5, 0.5),
TertiaryRds = c(0.015, 0.188, 0.164, 0.164),
Deer = c(0.087, 0, 0.057, 0.057),
Moose = c(0.112, 0.382, 0.385, 0.385),
east = c(1358690L, 1346840L, 1343380L, 1343380L),
north = c(12086700L, 12099300L, 12100000L, 12100000L)
)
euc.dist <- function(x)
{
sqrt((1440000-x[1])^2 + (12160000-x[2])^2)
}
df <- cbind(df, dist = apply(df[,c("east", "north")],1, euc.dist))
#> ID PackNumber StudyArea CoyoteAncestry(Logit) PrimaryRds SecondaryRds
#> 1 49-2 1 Out -0.8344735 0.088 0.499
#> 2 49-3 2 Out -2.4088543 0.000 0.302
#> 3 49-4 2 Out -3.8969349 0.000 0.500
#> 4 49-7 2 Out -2.6995486 0.000 0.500
#> TertiaryRds Deer Moose east north dist
#> 1 0.015 0.087 0.112 1358690 12086700 109472.4
#> 2 0.188 0.000 0.382 1346840 12099300 111190.3
#> 3 0.164 0.057 0.385 1343380 12100000 113734.0
#> 4 0.164 0.057 0.385 1343380 12100000 113734.0
推荐阅读
- node.js - 从浏览器地址栏到 Node.js 终端的实时输入
- node.js - 使 Node.js ftp-srv 只读,无需命令行
- python - 如何在不等待python的情况下使用套接字
- javascript - Javascript:当 URL 更改时更新内部 html
- python - 如何在Networkx中漂亮地画出毛毛虫树?
- php - 在 PHP 中获取响应 cookie 作为关联数组
- c - 为什么我在 macOS Catalina 的 /usr/lib/libSystem.dylib 文件中找不到“printf”?
- python - 'int' 类型的参数不是需要可迭代的帮助
- dialogflow-es - Dialogflow 中的 Twilio(文本消息)
- named-entity-recognition - spacy - 3.1 自定义损失函数和数据增强,用于不平衡数据的命名实体识别