R 如何在给定行中找到包含最小值的列?
我在R中有数据帧df:R 如何在给定行中找到包含最小值的列?,r,dataframe,R,Dataframe,我在R中有数据帧df: accuracy method A 3 method B 6 method C 2 method D 8 method E 1 如何返回精度最高的方法的名称?@r2evans已经回答了这个问题,但是,这里是正式的答案 df <- data.frame(method = letters[1:5], accuracy = c(2,6,2,8,1), stringsAsFactors = F) &
accuracy
method A 3
method B 6
method C 2
method D 8
method E 1
如何返回精度最高的方法的名称?@r2evans已经回答了这个问题,但是,这里是正式的答案
df <- data.frame(method = letters[1:5], accuracy = c(2,6,2,8,1),
stringsAsFactors = F)
> df
method accuracy
1 a 2
2 b 6
3 c 2
4 d 8
5 e 1
# method 1, get the row index of the max value for a column
> which.max(df$accuracy)
[1] 4
# method 2, return the entire row where the column accuracy
# has a maximal value
library(dplyr)
df %>%
filter(accuracy == max(accuracy))
method accuracy
1 d 8
df[which.maxdf$accurity,]将为您提供行,您可能需要行名……也可以看看这个。使用dplyr,您可以将group_by与top_n一起使用,也可以将filter与max一起使用。@r2evans也有一个很好的答案,即.max.@Kamil,如果我从问题中推断出该方法是在行的名称中标识的,那么dplyr将自动删除行的名称。而且OP似乎没有试图对它们进行分组,因此group_by已被取消。标题看起来像是另一个问题,如果OP依赖它们,您不妨将tibble::ROWNAME_引用到_columndf。