Message-ID: <de4e29f50911050650g5009c829s242de8fa2beba4f6@mail.gmail.com>
Date: 2009-11-05T14:50:19Z
From: Baptiste Auguie
Subject: How to refer to the last a few rows?
In-Reply-To: <366c6f340911050617i5b2b833l9a109b0ad7b7ce56@mail.gmail.com>
>From utils:::tail.data.frame, you could define your own,
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>:
> On Thu, Nov 5, 2009 at 7:24 AM, jim holtman <jholtman at gmail.com> wrote:
>> will this do:
>>
>>> x
>> ? ? [,1] [,2] [,3] [,4] [,5]
>> [1,] ? ?1 ? ?6 ? 11 ? 16 ? 21
>> [2,] ? ?2 ? ?7 ? 12 ? 17 ? 22
>> [3,] ? ?3 ? ?8 ? 13 ? 18 ? 23
>> [4,] ? ?4 ? ?9 ? 14 ? 19 ? 24
>> [5,] ? ?5 ? 10 ? 15 ? 20 ? 25
>>> tail(x[,tail(seq(ncol(x),3))], 2)
>> ? ? [,1] [,2] [,3]
>> [4,] ? 24 ? 19 ? 14
>> [5,] ? 25 ? 20 ? 15
>>>
>
> I would avoid to type the variable name twice. Because this is very
> inconvenient when the variable name is long.
>
> a_loooooooooooooooooong_expression_or_variable[nrow(a_loooooooooooooooooong_expression_or_variable),]
>
> BTW, you misunderstood my question. My question was whether is a
> better way to get the last a few columns than 't(tail(t(x),2))'.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>