【问题标题】:efficiently finding first nonzero element (corresponding column) of a data table有效地找到数据表的第一个非零元素(对应列)
【发布时间】:2019-04-22 02:13:18
【问题描述】:

对于以下类型的问题,堆栈上有一些答案,但它们都效率低下并且不能很好地扩展。

要重现它,假设我有如下数据:

    tempmat=matrix(c(1,1,0,4,1,0,0,4,0,1,0,4, 0,1,1,4, 0,1,0,5),5,4,byrow=T)
    tempmat=rbind(rep(0,4),tempmat)
    tempmat=data.table(tempmat)
    names(tempmat)=paste0('prod1vint',1:4)

这就是数据的样子,尽管它要大得多,因此解决方案不能是“应用”或基于行的方法。

> tempmat
   prod1vint1 prod1vint2 prod1vint3 prod1vint4
1:          0          0          0          0
2:          1          1          0          4
3:          1          0          0          4
4:          0          1          0          4
5:          0          1          1          4
6:          0          1          0          5

我想识别第一个非零元素的列,所以输出如下所示:

> tempmat
   prod1vint1 prod1vint2 prod1vint3 prod1vint4 firstnonzero
1:          0          0          0          0           NA
2:          1          1          0          4            1
3:          1          0          0          4            1
4:          0          1          0          4            2
5:          0          1          1          4            2
6:          0          1          0          5            2

【问题讨论】:

    标签: r dplyr data.table


    【解决方案1】:

    一种选择是使用rowSumsmax.col 指定ties.method = "first"

    temp <- tempmat != 0
    (NA^(rowSums(temp) == 0)) * max.col(temp, ties.method = "first")
    #[1] NA  1  1  2  2  2
    

    max.col 将给出每行中第一个最大值的列索引。但是,如果所有值都是 0(如第一行),这将返回 1,因为 0 是该行中的最大值。为避免这种情况,我们使用rowSums 检查行中是否至少有一个非零值,并将其乘以max.col 输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      相关资源
      最近更新 更多