How to refer to the last a few rows?

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

How to refer to the last a few rows?

Peng Yu
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to refer to the last a few rows?

baptiste auguie-5
Hi,


tail(x,2)

or

 x[seq(nrow(x)-1, nrow(x)), ]


HTH,

baptiste

2009/9/14 Peng Yu <[hidden email]>

> 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
>
> ______________________________________________
> [hidden email] 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
______________________________

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to refer to the last a few rows?

Federman, Douglas
In reply to this post by Peng Yu
Try the tail() function

-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
On Behalf Of Peng Yu
Sent: Monday, September 14, 2009 11:32 AM
To: [hidden email]
Subject: [R] How to refer to the last a few rows?

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

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to refer to the last a few rows?

Bert Gunter
?nrow

Bert Gunter
Genentech Nonclinical Biostatistics

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On
Behalf Of Federman, Douglas
Sent: Monday, September 14, 2009 9:03 AM
To: Peng Yu; [hidden email]
Subject: Re: [R] How to refer to the last a few rows?

Try the tail() function

-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
On Behalf Of Peng Yu
Sent: Monday, September 14, 2009 11:32 AM
To: [hidden email]
Subject: [R] How to refer to the last a few rows?

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

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to refer to the last a few rows?

Giovanni Petris
In reply to this post by Peng Yu

> Date: Mon, 14 Sep 2009 10:32:09 -0500
> From: Peng Yu <[hidden email]>
>
> 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

But R does known the total number of rows: it's NROW(x) -- or nrow(x)
if you are sure 'x' is a matrix


HTH,
Giovanni

> the last 2 rows?
>
> Regards,
> Peng
>
> ______________________________________________
> [hidden email] 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.
>
>

--

Giovanni Petris  <[hidden email]>
Associate Professor
Department of Mathematical Sciences
University of Arkansas - Fayetteville, AR 72701
Ph: (479) 575-6324, 575-8630 (fax)
http://definetti.uark.edu/~gpetris/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to refer to the last a few rows?

Peng Yu
In reply to this post by baptiste auguie-5
On Mon, Sep 14, 2009 at 9:37 AM, baptiste auguie
<[hidden email]> 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 <[hidden email]>
>>
>> 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
>>
>> ______________________________________________
>> [hidden email] 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
> ______________________________
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to refer to the last a few rows?

jholtman
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 <[hidden email]> wrote:

> On Mon, Sep 14, 2009 at 9:37 AM, baptiste auguie
> <[hidden email]> 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 <[hidden email]>
>>>
>>> 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
>>>
>>> ______________________________________________
>>> [hidden email] 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
>> ______________________________
>>
>>
>
> ______________________________________________
> [hidden email] 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?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to refer to the last a few rows?

Peng Yu
On Thu, Nov 5, 2009 at 7:24 AM, jim holtman <[hidden email]> 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))'.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to refer to the last a few rows?

baptiste auguie-5
>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 <[hidden email]>:

> On Thu, Nov 5, 2009 at 7:24 AM, jim holtman <[hidden email]> 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))'.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to refer to the last a few rows?

jholtman
In reply to this post by Peng Yu
With the flexibility of R, you can always create a function that will
allow you to avoid retyping.

On Thu, Nov 5, 2009 at 9:17 AM, Peng Yu <[hidden email]> wrote:

> On Thu, Nov 5, 2009 at 7:24 AM, jim holtman <[hidden email]> 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))'.
>
> ______________________________________________
> [hidden email] 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?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to refer to the last a few rows?

Ben Bolker
In reply to this post by Peng Yu

Peng Yu wrote
On Thu, Nov 5, 2009 at 7:24 AM, jim holtman <jholtman@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))'.
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 ...