Calculate the difference between the largest and smallest column for each row

后端 未结 2 1573
暖寄归人
暖寄归人 2021-01-21 23:46

The title is pretty straight forward - how can I calculate the difference between the largest and smallest value column-wise, for each row?

Let\'s assume this is my data

2条回答
  •  悲哀的现实
    2021-01-22 00:15

    Here's an attempt using my old favourite max.col with a bit of matrix indexing:

    rw <- seq_len(nrow(dat))
    dat[cbind(rw, max.col(dat))] - dat[cbind(rw, max.col(-dat))]
    #[1] 3 9 3 3
    

    This should be much faster on large datasets, as per:

    # 5 million big enough?
    dat <- dat[sample(1:4,5e6,replace=TRUE),]
    
    system.time({
      rw <- seq_len(nrow(dat))
      dat[cbind(rw, max.col(dat))] - dat[cbind(rw, max.col(-dat))]
    })
    #   user  system elapsed 
    #   2.43    0.20    2.63 
    
    system.time({
      apply(X = dat, MARGIN = 1, function(x) diff(range(x)))
    })
    #   user  system elapsed 
    #  94.91    0.17   95.16 
    

提交回复
热议问题