See dist()
.
(参见dist()
。)
Since not enough test data was provided, here's an example on iris
: (由于没有提供足够的测试数据,因此以下是关于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
And a similar approach in dplyr
(和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
And data.table :
(和data.table :)
library(data.table)
as.data.table(iris)[,
.(max_dist = max(dist(.SD))),
.SDcols = c('Sepal.Length', 'Sepal.Width'),
by = Species]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…