【问题标题】:which.max() to extract the last largest valuewhich.max() 提取最后一个最大值
【发布时间】:2021-10-18 23:40:04
【问题描述】:

在我的代码中,我想提取一系列列中的最后一个最大值,并在数据帧上逐行迭代。问题是我的脚本只给了我第一次出现的最大值(在这种情况下是第一列的名称)。关于如何更新此行以获得最后一个最大值的任何建议?

我的数据框如下所示:

A.trust B.trust C.trust D.trust E.trust F.trust G.trust H.trust I.trust J.trust K.trust L.trust M.trust
-999    -999    -999    -999    -999    -999    -999.0  -999.0  -999.0   -999   -999    -999    -999
-999    -999    -999    -999    -999    -999     0.5    -999.0   0.5     -999   -999    -999    -999
-999    -999    -999    -999    -999    -999    -999.0  -999.0   1.0     -999   -999    -999    -999
-999    -999    -999    -999    -999    -999    -999.0  -999.0  -999.0   -999   -999    -999    -999
-999    -999    -999    -999    -999    -999    -999.0  -999.0  -999.0   -999   -999    -999    -999
-999    -999    -999    -999    -999    -999     0.5     0.5    -999.0   -999   -999    -999    -999

我正在使用以下代码

# return cols which hold maximum
nams <- names(test)[apply(test, 1 ,which.max)]

电流输出

目前nams变量中的值为:

"A.trust"
"G.trust" 
"I.trust" 
"A.trust"
"A.trust" 
"G.trust"

需要的输出

nams 变量中我需要的值是这样的:

"M.trust" 
"I.trust" 
"I.trust"
"M.trust" 
"M.trust" 
"H.trust"

输入()

test = structure(list(A.trust = c(-999, -999, -999, -999, -999, -999), 
               B.trust = c(-999, -999, -999, -999, -999, -999), 
               C.trust = c(-999, -999, -999, -999, -999, -999), 
               D.trust = c(-999, -999, -999, -999, -999, -999), 
               E.trust = c(-999, -999, -999, -999, -999, -999), 
               F.trust = c(-999, -999, -999, -999, -999, -999), 
               G.trust = c(-999,  0.5, -999, -999, -999,  0.5), 
               H.trust = c(-999, -999, -999, -999, -999,  0.5), 
               I.trust = c(-999,  0.5,  1,   -999, -999, -999), 
               J.trust = c(-999, -999, -999, -999, -999, -999), 
               K.trust = c(-999, -999, -999, -999, -999, -999), 
               L.trust = c(-999, -999, -999, -999, -999, -999), 
               M.trust = c(-999, -999, -999, -999, -999, -999)), 
          row.names = 600:605, class = "data.frame")

相关帖子

以下帖子与我的问题相关,但我正在努力解决如何在我的脚本How to find the last occurrence of a certain observation in grouped data in R? 中进行此更改。这是另一个相关的帖子which.max ties method in R。 任何帮助将不胜感激!

【问题讨论】:

  • names(test)[max.col(test, ties.method = "last")]

标签: r max rowwise


【解决方案1】:
names(rev(test))[apply(rev(test), 1, which.max)]
[1] "M.trust" "I.trust" "I.trust" "M.trust" "M.trust" "H.trust"

【讨论】:

    【解决方案2】:

    另一种可能性(可能比@Skaqqs的回答效率低):

    names(test)[apply(test, 1, function(x) tail(which(x == max(x)), 1)]
    

    【讨论】:

    • 谢谢,@Ben Bolker。但是代码导致我的错误。
    【解决方案3】:

    我们可以使用矢量化选项

     names(test)[ncol(test):1][max.col(rev(test), "first")]
    [1] "M.trust" "I.trust" "I.trust" "M.trust" "M.trust" "H.trust"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      相关资源
      最近更新 更多