Skip to content
Prev 199241 / 398503 Next

How to refer to the last a few rows?

Peng Yu wrote:
Maybe not. I would say a reasonably standard R-ish way would
be

nc <- ncol(x); x[,(nc-5):nc]

Re: really long variable names, perhaps changing your variable naming style
would
be helpful?

If you frequently want the last few columns, then maybe writing

tailcol <- function(x,n=5) {
   nc <- ncol(x); n <- max(0,nc-n); x[,(nc-n):nc]
}
(or something like that; you should decide what you want to happen
when n>nc)

and putting it in your own personal set of utilities would be helpful ...