Return row of Data Frame based on value in a column - R

brno792

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?

Kara Woo

@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)), ]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

R - Assign column value based on closest match in second data frame

From Dev

R add index column to data frame based on row values

From Dev

Select column from data frame based on dynamic value in R

From Dev

Multiply a data frame column with a single row in another data frame, R

From Dev

R: return a value from one column in a data frame corresponding to the minimum value in another column

From Dev

Selecting value based on variable name of data frame column in R

From Dev

Clean R data frame so that in a column no row value is bigger than 2 times next row value

From Dev

Filling 3rd column based on value of 2nd column in data frame in R

From Dev

R: return row and column numbers of matches in a data frame

From Dev

R data frame, for every row, how to divide value in specified column by n and store in another column?

From Dev

Copy columns of a data frame based on the value of a third column in R

From Dev

Generating sub data frame based on a value in an column

From Dev

Add column to data frame based on values of another column in another row

From Dev

R: Replace Column Name with Row Name on Bases of a Value in Data Frame

From Dev

R add index column to data frame based on row values

From Dev

Select column from data frame based on dynamic value in R

From Dev

Return column index of first set of consecutive values in data frame row in R

From Dev

Selecting value based on variable name of data frame column in R

From Dev

Filling 3rd column based on value of 2nd column in data frame in R

From Dev

Searching for row with string value in R data frame

From Dev

Return value pairs based on first row and column

From Dev

Copy columns of a data frame based on the value of a third column in R

From Dev

How to add data from column in a data frame to a corpus based on a value from another column in R?

From Dev

Merge data frame based on column names in r

From Dev

Return row of Data Frame based on value in a column. R script

From Dev

R data frame subsetting based on a column value frequency threshold

From Dev

R: Aggregating a list of column names mapped to row numbers based off of a condition in a data frame

From Dev

r - Replace values in a data.frame column with a different value in the same column based unique ID

From Dev

In an R data frame, for a given row, how can I find what percentage a value in column A is of a value in column B?

Related Related

  1. 1

    R - Assign column value based on closest match in second data frame

  2. 2

    R add index column to data frame based on row values

  3. 3

    Select column from data frame based on dynamic value in R

  4. 4

    Multiply a data frame column with a single row in another data frame, R

  5. 5

    R: return a value from one column in a data frame corresponding to the minimum value in another column

  6. 6

    Selecting value based on variable name of data frame column in R

  7. 7

    Clean R data frame so that in a column no row value is bigger than 2 times next row value

  8. 8

    Filling 3rd column based on value of 2nd column in data frame in R

  9. 9

    R: return row and column numbers of matches in a data frame

  10. 10

    R data frame, for every row, how to divide value in specified column by n and store in another column?

  11. 11

    Copy columns of a data frame based on the value of a third column in R

  12. 12

    Generating sub data frame based on a value in an column

  13. 13

    Add column to data frame based on values of another column in another row

  14. 14

    R: Replace Column Name with Row Name on Bases of a Value in Data Frame

  15. 15

    R add index column to data frame based on row values

  16. 16

    Select column from data frame based on dynamic value in R

  17. 17

    Return column index of first set of consecutive values in data frame row in R

  18. 18

    Selecting value based on variable name of data frame column in R

  19. 19

    Filling 3rd column based on value of 2nd column in data frame in R

  20. 20

    Searching for row with string value in R data frame

  21. 21

    Return value pairs based on first row and column

  22. 22

    Copy columns of a data frame based on the value of a third column in R

  23. 23

    How to add data from column in a data frame to a corpus based on a value from another column in R?

  24. 24

    Merge data frame based on column names in r

  25. 25

    Return row of Data Frame based on value in a column. R script

  26. 26

    R data frame subsetting based on a column value frequency threshold

  27. 27

    R: Aggregating a list of column names mapped to row numbers based off of a condition in a data frame

  28. 28

    r - Replace values in a data.frame column with a different value in the same column based unique ID

  29. 29

    In an R data frame, for a given row, how can I find what percentage a value in column A is of a value in column B?

HotTag

Archive