Jeff
Jeff

Reputation: 8411

How to find the indexes of the minimum value in whole dataframe?

Assume i have the following dataset

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

and i need to find the indexes of minimum value in whole dataframe? (row, and column)

Upvotes: 2

Views: 1900

Answers (1)

d.b
d.b

Reputation: 32538

Use which and set arr.ind = TRUE to get the row and column. Also set na.rm = TRUE so that the missing values are removed when obtaining the minimum.

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

Upvotes: 6

Related Questions