【问题标题】:R return the index of the minimum column for each rowR返回每行最小列的索引
【发布时间】:2013-08-21 20:31:18
【问题描述】:

我有一个包含 4 列的 data.frame(如下所示)。我想找到每一行的最小列(不是值)的索引。知道如何实现这一目标吗?

> d
            V1         V2         V3         V4
1  0.388116155 0.98999967 0.41548536 0.76093748
2  0.495971331 0.47173142 0.51582728 0.06789924
3  0.436495321 0.48699268 0.21187838 0.54139290
4  0.313514389 0.50265539 0.08054103 0.46019601
5  0.277275961 0.39055360 0.29594162 0.70622532
6  0.264804739 0.86996266 0.85708635 0.61136741
7  0.627344463 0.54277873 0.96769568 0.80399490
8  0.814420492 0.35362949 0.39023446 0.39246250
9  0.517459983 0.65895805 0.93662382 0.06762166
10 0.498319937 0.67081260 0.43225997 0.42139151
11 0.046862110 0.97304915 0.06542971 0.09779383
12 0.619009734 0.82363618 0.14514799 0.52858058
13 0.007262782 0.82203403 0.08573499 0.61094206
14 0.001602586 0.33241230 0.57762669 0.45285004
15 0.698388370 0.83541257 0.21051568 0.84431347
16 0.296088411 0.34363164 0.02179999 0.70551493
17 0.897869571 0.50625928 0.92861583 0.61249019
18 0.372497428 0.29025182 0.23201891 0.55737699
19 0.172931860 0.03604668 0.50291560 0.10850847
20 0.988827604 0.15800337 0.87999839 0.09899663

所以我想要以下输出:

1    1
2    4
3    3
4    3

继续所有行。 谢谢

【问题讨论】:

    标签: r indexing apply minimum rowwise


    【解决方案1】:

    您的英文描述表明您想要:

     apply( df, 1, which.min)
    

    但是如果上述解释正确,您给出的答案未格式化为向量,也不是正确答案。哦等等,你期待的是行号。

     as.matrix(apply( d, 1, which.min))
    
       [,1]
    1     1
    2     4
    3     3
    4     3
    5     1
    6     1
    7     2
    8     2
    9     4
    10    4
    11    1
    12    3
    13    1
    14    1
    15    3
    16    3
    17    2
    18    3
    19    2
    20    4
    

    【讨论】:

    • 我正在寻找每列中的最小索引,并且能够从您的答案中推断出它是apply( df, 2, which.min)。如果这对其他人有帮助。
    【解决方案2】:

    另一个选项是max.cold 乘以-1

    max.col(-d)
    # [1] 1 4 3 3 1 1 2 2 4 4 1 3 1 1 3 3 2 3 2 4
    

    如果你需要一个矩阵作为输出,使用

    cbind(1:nrow(d),    # row
          max.col(-d))  # column position of minimum
    

    这是两种方法的基准

    set.seed(42)
    dd <- as.data.frame(matrix(runif(1e5 * 100), nrow = 1e5, ncol = 100))
    
    library(microbenchmark)
    library(ggplot2)
    
    b <- microbenchmark(
      apply = apply(dd, 1, which.min),
      max_col = max.col(-dd),
      times = 25
    )
    
    autoplot(b)
    

    b
    #Unit: milliseconds
    #    expr      min       lq     mean   median       uq       max neval cld
    #   apply 705.7478 855.7112 906.2340 892.3214 933.4655 1211.5016    25   b
    # max_col 162.8273 175.6363 227.1156 206.0213 225.2973  406.9124    25  a 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-15
      • 2018-08-29
      • 2016-09-16
      • 2014-07-23
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多