【问题标题】:How to find the indexes of the minimum value in whole dataframe?如何在整个数据框中找到最小值的索引?
【发布时间】:2017-09-25 13:19:51
【问题描述】:

假设我有以下数据集

dt<-data.frame(id=1:10,X=sample(10),Y=sample(10))
dt<-as.data.frame(as.matrix(dist(dt)))
for(i in 1:nrow(dt))
  dt[i,i]<-NA

我需要在整个数据帧中找到最小值的索引?(行和列)

【问题讨论】:

    标签: r


    【解决方案1】:

    使用which 并设置arr.ind = TRUE 来获取行和列。还要设置na.rm = TRUE,以便在获取最小值时去掉缺失值。

    which(dt == min(dt, na.rm = TRUE), arr.ind = TRUE)
    #  row col
    #2   2   1
    #1   1   2
    

    【讨论】:

      猜你喜欢
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 2020-12-20
      • 2021-04-28
      • 2017-09-19
      • 2021-04-20
      • 2020-06-12
      相关资源
      最近更新 更多