r - R中13个行索引之间欧几里得距离的排列
问题描述
我有 13 个感兴趣区域 (AOI),每个测试图像都有 x 和 y 值。如何获得 AOI 对 ((x-xi)+(y-yi))^(1/2) 之间欧几里得距离的所有可能组合?最终,我正在寻找每个测试图像的两个 AOI 之间所有可能距离的最大距离。
这可以在不使用循环的情况下完成吗?
> setwd("C:/Users/Data/Desktop")
> RawColor <- read.csv(file="13ptColor.csv")
> print(RawColor)
SN TestImage AOI x y
1 50293253 B 13 0.1597 0.06775
2 50293253 B 12 0.1587 0.06574
3 50293253 B 11 0.1596 0.06715
4 50293253 B 10 0.1594 0.06618
5 50293253 B 9 0.1590 0.06582
6 50293253 B 8 0.1593 0.06638
7 50293253 B 7 0.1589 0.06602
8 50293253 B 6 0.1594 0.06601
9 50293253 B 5 0.1591 0.06552
10 50293253 B 4 0.1587 0.06473
11 50293253 B 3 0.1593 0.06603
12 50293253 B 2 0.1585 0.06481
13 50293253 B 1 0.1588 0.06510
14 50293253 G 13 0.2985 0.60400
15 50293253 G 12 0.2977 0.60440
解决方案
见dist()
。由于没有提供足够的测试数据,这里有一个例子iris
:
as.matrix(
by(data = iris[, c('Sepal.Length', 'Sepal.Width')],
INDICES = iris[, 'Species', drop = F],
FUN = function(DF) max(dist(DF))
)
)
# [,1]
# setosa 2.418677
# versicolor 2.332381
# virginica 3.269557
# or
sp_DF <- split(x = iris[, c('Sepal.Length', 'Sepal.Width')],
f = iris[, 'Species', drop = F])
sapply(sp_DF, function(DF) max(dist(DF)))
# setosa versicolor virginica
# 2.418677 2.332381 3.269557
和dplyr中的类似方法
library(dplyr)
iris%>%
group_by(Species)%>%
summarize(max_dist = max(dist(cbind(Sepal.Length, Sepal.Width))))
# A tibble: 3 x 2
Species max_dist
<fct> <dbl>
1 setosa 2.42
2 versicolor 2.33
3 virginica 3.27
library(data.table)
as.data.table(iris)[,
.(max_dist = max(dist(.SD))),
.SDcols = c('Sepal.Length', 'Sepal.Width'),
by = Species]
推荐阅读
- sql - 在 MS Access Query 中使用多选列表框中的值
- batch-file - 为什么这条将文件从一个驱动器复制到另一个驱动器的批处理行只完成了一半?
- git - 将合并恢复为主,保留功能分支更改,而不更改历史记录
- ansible - 限制 shell 命令结果列表中的 ansible 输出
- intellij-idea - IntelliJ 运行 maven 任务并同时启动 jar?
- ansible - ansible - 类型不正确。预期的“对象”
- wpf - 在 wpf 应用程序中删除 shift+space 上的网格列
- java - Android Studio:运行我的程序时出错
- if-statement - 在 Google 表格中使用 IF 函数进行复杂查询
- java - 从其他数组中提取二维数组