oak tree
oak tree

Reputation: 13

find minimum of 2 columns from a data frame (minimize 2 columns at the same time) in R

I have a data frame like this:

X Y
1 2
3 1
1 1
2 3
1 2

Now I want to find the minimum value of X and among the smallest values for X I want to pick the row that has the smallest value for Y. (My data has several minima.) So in this example the desired output is "line 3" because minimum value of X is 1 and among the rows with X=1 the minimum value for Y is in line 3 (Y=1). I know the function min() which seems to pick the first minimal value of the data.frame or of the specified column of the data.frame.

But is there a function in R or an easy way to find the row that minimizes X and Y at the same time?

Right now I would

  1. use the min() function to find the minimum value for X, then
  2. remove every row that has a greater value for X than the minimum
  3. use the min() function to find the minimum value for Y (among these remaining lines)
  4. see how I find the corresponding row in the original data.frame.

But there must be a more easy way to do it?

Upvotes: 0

Views: 528

Answers (3)

ThomasIsCoding
ThomasIsCoding

Reputation: 101034

Another base R option

> df[which.min(as.integer(interaction(df))), ]
  X Y
3 1 1

or a data.table option

> setorder(setDT(df))[1]
   X Y
1: 1 1

Upvotes: 0

dy_by
dy_by

Reputation: 1241

use data.table package

library(dplyr)
library(data.table)

dt <- read.table(text = "X Y
1 2
3 1
1 1
2 3
1 2", header = T)

dt <- dt %>% as.data.table() ## convert to data.table
dt[X == min(X), .SD[Y == min(Y)]][1]

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388797

If you arrange the data by X and Y, you can select the 1st row of the dataframe.

In dplyr that would be -

library(dplyr)

df %>% arrange(X, Y) %>% slice(1L)

#  X Y
#1 1 1

Or in base R -

df[order(df$X, df$Y)[1], ]

Upvotes: 2

Related Questions