36

My R data.frame df looks like:

 Name Amount 1 "A" 150 2 "B" 120 3 "C" "NA" 4 "D" 160 . . . 

I want to get the Name and Amount row when I do something like min(df$Amount).

That gets me the minimum number in the Amount column, but how do I get the Name in that same row? Or the entire row for that matter?

Name should be "B" in this case.

Similar to Select * Where Amount = min(Amount)

What is the best way to do this in R?

1
  • 2
    Subsetting is quite important to learn if you really want to use R efficiently, so perhaps you want to take a look at An Introduction to R to get started. Commented Jul 18, 2014 at 18:37

4 Answers 4

47

@Zelazny7's answer works, but if you want to keep ties you could do:

df[which(df$Amount == min(df$Amount)), ] 

For example with the following data frame:

df <- data.frame(Name = c("A", "B", "C", "D", "E"), Amount = c(150, 120, 175, 160, 120)) df[which.min(df$Amount), ] # Name Amount # 2 B 120 df[which(df$Amount == min(df$Amount)), ] # Name Amount # 2 B 120 # 5 E 120 

Edit: If there are NAs in the Amount column you can do:

df[which(df$Amount == min(df$Amount, na.rm = TRUE)), ] 
Sign up to request clarification or add additional context in comments.

Comments

5

Use which.min:

df <- data.frame(Name=c('A','B','C','D'), Amount=c(150,120,175,160)) df[which.min(df$Amount),] > df[which.min(df$Amount),] Name Amount 2 B 120 

From the help docs:

Determines the location, i.e., index of the (first) minimum or maximum of a numeric (or logical) vector.

5 Comments

What if I have NA data in my column? Can I use na.rm=TRUE in which.min argument? Doesn't appear to work.
you don't have to use na.rm at all. Add some NAs to the data.frame I constructed and see for yourself. You can check the functionality with this which.min(c(rep(NA, 10), c(1:5)))
yeah but if I take Max, then it returns the NA value. So how would I do this if I want the max instead of the min?
um, no, it doesn't! Can you post some code of what you're experiencing?
it does if I just do max(df$Amount). You're right, not when using which.max, but get a NAs induced by coercion warning.
3

Based on the syntax provided

 Select * Where Amount = min(Amount) 

You could do using:

 library(sqldf) 

Using @Kara Woo's example df

 sqldf("select * from df where Amount in (select min(Amount) from df)") #Name Amount #1 B 120 #2 E 120 

1 Comment

That's a simple cheat for developers used to standard programming protocols, but how is performance?
2

You could use dplyr:

df %>% group_by("Amount") %>% slice(which.min(x)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.