Skip to content
Prev 199232 / 398503 Next

How to refer to the last a few rows?

tail =
function (x, n = 6L, colwise=FALSE, ...)
{
    stopifnot(length(n) == 1L)
    nrx <- if(colwise) ncol(x) else nrow(x)
    n <- if (n < 0L)
        max(nrx + n, 0L)
    else min(n, nrx)
if (colwise)
  x[ ,seq.int(to = nrx, length.out = n), drop = FALSE] else
  x[seq.int(to = nrx, length.out = n), , drop = FALSE]
}

m = matrix(1:30, 5)

tail(m,2)
tail(m, 2, colwise=TRUE)


HTH,

baptiste

2009/11/5 Peng Yu <pengyu.ut at gmail.com>: