[R] Automatic routine - help

Richard A. O'Keefe
Thu, 24 Jun 2004 01:12:06 -0000

I note that ?min actually contains a relevant example:

    plot(x, pmin(cH, pmax(-cH, x)), type='b', main= "Huber's
function")
            ^^^^^^^^^^^^^^^^^^^^^^

It's always worth timing things.  The timing tests below are the best
of several repetitions, to avoid worries about things like paging and GC.

Make a data frame with 10 variables and 2000 cases.

    > m <- matrix(rnorm(10*2000), ncol=10)
    > colnames(m) <- LETTERS[1:10]
    > z <- as.data.frame(m)

See how long it takes to just make a copy of z.

    > system.time(w <- z+0)
    [1] 0.08 0.01 0.16 0.00 0.00

See how long pmax(pmin(...)...) takes.

    > system.time(w <- pmax(pmin(z, 1), 0))
    [1] 0.08 0.01 0.16 0.00 0.00

See how long the z[z < 0] <- 0; z[z > 1] <- 1 trick takes.

    > system.time({ w <- z; w[w < 0] <- 0; w[w > 1] <- 1 })
    [1] 0.23 0.01 0.31 0.00 0.00