[R] How to refer to the last a few rows?

jim holtman jholtman at gmail.com
Thu Nov 5 14:24:34 CET 2009


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
>


On Thu, Nov 5, 2009 at 8:05 AM, Peng Yu <pengyu.ut at gmail.com> wrote:
> On Mon, Sep 14, 2009 at 9:37 AM, baptiste auguie
> <baptiste.auguie at googlemail.com> wrote:
>> Hi,
>>
>>
>> tail(x,2)
>
> Is there a convenient function like 'tail()' to extract last a few
> columns? I can use t(tail(t(x),2)). But I feel convenient if there is
> a function can give me such thing directly.
>
>> or
>>
>>  x[seq(nrow(x)-1, nrow(x)), ]
>>
>>
>> HTH,
>>
>> baptiste
>>
>> 2009/9/14 Peng Yu <pengyu.ut at gmail.com>
>>>
>>> Hi,
>>>
>>> x=matrix(1:60,nr=6)
>>>
>>> I can refer the last 2 rows by
>>> x[5:6,]
>>>
>>> If I don't know the total number of rows is 6, is there a way to refer
>>> the last 2 rows?
>>>
>>> Regards,
>>> Peng
>>>
>>> ______________________________________________
>>> 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.
>>
>>
>>
>> --
>> _____________________________
>>
>> Baptiste Auguié
>>
>> School of Physics
>> University of Exeter
>> Stocker Road,
>> Exeter, Devon,
>> EX4 4QL, UK
>>
>> http://newton.ex.ac.uk/research/emag
>> ______________________________
>>
>>
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list