如何根据 r 中的聚集输出列从列中查找最大值

How to find max value from a column based on a clustered output column in r

我有一个数据框如下图

       X       Y      Z          cluster
245 256882.0 4110945 426.50          20
246 256882.7 4110945 426.42          57
247 256883.9 4110945 429.30         114
248 256884.6 4110945 428.93         114
249 256885.4 4110945 429.50          98
250 256886.1 4110945 429.67          33

数据框有 4 列 x、y、z 和集群输出。 xy是坐标,z是对应的高度。我使用 kmeans 将整个数据点聚类为 176 个聚类。现在我想从每个集群中获取最大 z 值。例如,从簇值 1 中,我需要确定最大 z 值并且还需要采用相应的 x 和 y 值。我该怎么做?

您可以使用 dplyr:

library(dplyr)

data %>%
  group_by(fit.cluster) %>%
  summarise(Z = max(Z)) %>%
  inner_join(data)

或:

df %>% 
  group_by(fit.cluster) %>%
  filter(Z == max(Z))