Saturday, November 21, 2009

R trick to get column max

Learned from an email thread. This is the kind of problem you'll never think about if you are a student using R. Suppose you have a k by n matrix, say x, where k is small but n is huge (say 1,000,000), and you want to get the maximum in each column, that is, n different values.

The naive way of apply(x, 2, max) takes forever to complete.

The new trick I learned is:

# transpose x and convert to a data.frame
t.x <- as.data.frame(t(x))
# use do.call with "pmax" and the input matrix
x.max <- do.call("pmax", t.x)

No comments: