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
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