如何查找dataframe中特定列的哪个行具有最高值?

时间:2022-04-22 19:35:43

I don't know how to find which row would give me the highest value for a specific column in a data frame

我不知道如何找到哪一行会给我一个数据帧中特定列的最大值。

For example below.

例如下面。

mtcars 
               mpg cyl disp  hp drat   wt ... 
Mazda RX4     21.0   6  160 110 3.90 2.62 ... 
Mazda RX4 Wag 21.0   6  160 110 3.90 2.88 ... 
Datsun 710    22.8   4  108  93 3.85 2.32 ...

I am focusing on the wt column and trying to see which one has the highest value, which would be the Mazda RX4 Wag of 2.88

我关注的是wt列,并试着看哪一列的值最高,即马自达RX4 Wag的值为2.88

Also how do I find specific values using names instead of vectors? For example Mazda RX4 Wag wt. I have tried df[df$Mazda RX4 Wag,df$wt] but gives me an error.

如何用名称而不是向量来找到特定的值?例如马自达RX4 Wag wt.我尝试过df[df$Mazda RX4 Wag,df$wt],但是给了我一个错误。

Thanks

谢谢

4 个解决方案

#1


2  

To your fist question.

你的拳头的问题。

subset(mtcars, wt == max(wt))[, "wt"]
[1] 5.424

To your second question

你的第二个问题

mtcars[row.names(mtcars) == "Mazda RX4 Wag", "wt"]
[1] 2.875

#2


0  

Maybe something like this:

也许是这样的:

which(name.of.datatable == max(name.of.datatable[, 7]), arr.ind = T)

#3


0  

drop = FALSE lets you print the car name i.e. the row name where you have highest or lowest wt value

drop = FALSE允许您打印汽车名称,即您拥有最高或最低wt值的行名称

1. Highest Value of wt column

1。wt列的最大值

To print maximum (Using max() function) or highest value of wt column.

打印wt列的最大值(使用max()函数)或最大值。

 mtcars[mtcars$wt == max(mtcars$wt), "wt", drop = FALSE]
 #                        wt
 # Lincoln Continental 5.424

OR

Use which.max() as suggested by ycw.

使用ycw建议的.max()。

  mtcars[which.max(mtcars$wt), "wt", drop = FALSE]
  #                        wt
  # Lincoln Continental 5.424

2. Lowest value of wt column

2。wt列的最小值

To print minimum (using min() function) or lowest value of wt column

打印最小值(使用min()函数)或wt列的最小值。

 mtcars[mtcars$wt == min(mtcars$wt), "wt", drop = FALSE]
 #                 wt
 # Lotus Europa 1.513

OR

Use which.min() as suggested by ycw.

使用ycw建议的。min()。

 mtcars[which.min(mtcars$wt), "wt", drop = FALSE]
 #                 wt
 # Lotus Europa 1.513

#4


0  

If you want the row number of the maximum value, use which.max():

如果您想要最大值的行号,请使用which.max():

> which.max(mtcars$wt)
[1] 16

To get all the information in that row, use it to subset your data frame:

要获取该行中的所有信息,请使用它对数据框架进行子集划分:

> mtcars[which.max(mtcars$wt),]
                     mpg cyl disp  hp drat    wt  qsec vs am gear carb
Lincoln Continental 10.4   8  460 215    3 5.424 17.82  0  0    3    4

To select elements of a data.frame by name just insert them as strings. If you want multiple rows or columns you will have to put them into a vector with c().

要按名称选择data.frame的元素,只需将它们作为字符串插入即可。如果想要多行或多列,就必须将它们放入带有c()的向量中。

> mtcars['Mazda RX4 Wag','wt']
[1] 2.875

#1


2  

To your fist question.

你的拳头的问题。

subset(mtcars, wt == max(wt))[, "wt"]
[1] 5.424

To your second question

你的第二个问题

mtcars[row.names(mtcars) == "Mazda RX4 Wag", "wt"]
[1] 2.875

#2


0  

Maybe something like this:

也许是这样的:

which(name.of.datatable == max(name.of.datatable[, 7]), arr.ind = T)

#3


0  

drop = FALSE lets you print the car name i.e. the row name where you have highest or lowest wt value

drop = FALSE允许您打印汽车名称,即您拥有最高或最低wt值的行名称

1. Highest Value of wt column

1。wt列的最大值

To print maximum (Using max() function) or highest value of wt column.

打印wt列的最大值(使用max()函数)或最大值。

 mtcars[mtcars$wt == max(mtcars$wt), "wt", drop = FALSE]
 #                        wt
 # Lincoln Continental 5.424

OR

Use which.max() as suggested by ycw.

使用ycw建议的.max()。

  mtcars[which.max(mtcars$wt), "wt", drop = FALSE]
  #                        wt
  # Lincoln Continental 5.424

2. Lowest value of wt column

2。wt列的最小值

To print minimum (using min() function) or lowest value of wt column

打印最小值(使用min()函数)或wt列的最小值。

 mtcars[mtcars$wt == min(mtcars$wt), "wt", drop = FALSE]
 #                 wt
 # Lotus Europa 1.513

OR

Use which.min() as suggested by ycw.

使用ycw建议的。min()。

 mtcars[which.min(mtcars$wt), "wt", drop = FALSE]
 #                 wt
 # Lotus Europa 1.513

#4


0  

If you want the row number of the maximum value, use which.max():

如果您想要最大值的行号,请使用which.max():

> which.max(mtcars$wt)
[1] 16

To get all the information in that row, use it to subset your data frame:

要获取该行中的所有信息,请使用它对数据框架进行子集划分:

> mtcars[which.max(mtcars$wt),]
                     mpg cyl disp  hp drat    wt  qsec vs am gear carb
Lincoln Continental 10.4   8  460 215    3 5.424 17.82  0  0    3    4

To select elements of a data.frame by name just insert them as strings. If you want multiple rows or columns you will have to put them into a vector with c().

要按名称选择data.frame的元素,只需将它们作为字符串插入即可。如果想要多行或多列,就必须将它们放入带有c()的向量中。

> mtcars['Mazda RX4 Wag','wt']
[1] 2.875