Re: [R] mirror vector?

2006-07-28 Thread Patrick Burns
Another approach is:

mat[nrow(mat):1, ]

Patrick Burns
[EMAIL PROTECTED]
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

Jacques VESLOT wrote:

> > mat <- matrix(1:16,4,4)
> > mat
>  [,1] [,2] [,3] [,4]
>[1,]159   13
>[2,]26   10   14
>[3,]37   11   15
>[4,]48   12   16
> > apply(mat,2,rev)
>  [,1] [,2] [,3] [,4]
>[1,]48   12   16
>[2,]37   11   15
>[3,]26   10   14
>[4,]159   13
>
>---
>Jacques VESLOT
>
>CNRS UMR 8090
>I.B.L (2ème étage)
>1 rue du Professeur Calmette
>B.P. 245
>59019 Lille Cedex
>
>Tel : 33 (0)3.20.87.10.44
>Fax : 33 (0)3.20.87.10.31
>
>http://www-good.ibl.fr
>---
>
>
>[EMAIL PROTECTED] a écrit :
>  
>
>>Hello,
>>
>>I'm an absolut beginner with R and now I got a 2D vector with numbers. I 
>>would like to mirror this vector now by the rows (so that the first row 
>>becomes last, second becomes one before last, ...).
>>I don't know if there is any method I can use to do this.
>>Could you please help me?
>>
>>Antje
>>
>>  
>>-
>>Was Sie schon immer wissen wollten aber nie zu Fragen trauten? Yahoo! Clever 
>>hilft Ihnen.
>>  [[alternative HTML version deleted]]
>>
>>__
>>R-help@stat.math.ethz.ch 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.
>>
>>
>>
>
>__
>R-help@stat.math.ethz.ch 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.
>
>
>  
>

__
R-help@stat.math.ethz.ch 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.


Re: [R] mirror vector?

2006-07-28 Thread Uwe Ligges
Jacques VESLOT wrote:
>  > mat <- matrix(1:16,4,4)
>  > mat
>   [,1] [,2] [,3] [,4]
> [1,]159   13
> [2,]26   10   14
> [3,]37   11   15
> [4,]48   12   16
>  > apply(mat,2,rev)
>   [,1] [,2] [,3] [,4]
> [1,]48   12   16
> [2,]37   11   15
> [3,]26   10   14
> [4,]159   13


or a lot faster:

mat[nrow(mat):1, ]

Uwe Ligges




> ---
> Jacques VESLOT
> 
> CNRS UMR 8090
> I.B.L (2ème étage)
> 1 rue du Professeur Calmette
> B.P. 245
> 59019 Lille Cedex
> 
> Tel : 33 (0)3.20.87.10.44
> Fax : 33 (0)3.20.87.10.31
> 
> http://www-good.ibl.fr
> ---
> 
> 
> [EMAIL PROTECTED] a écrit :
>> Hello,
>>
>> I'm an absolut beginner with R and now I got a 2D vector with numbers. I 
>> would like to mirror this vector now by the rows (so that the first row 
>> becomes last, second becomes one before last, ...).
>> I don't know if there is any method I can use to do this.
>> Could you please help me?
>>
>> Antje
>>
>>  
>> -
>> Was Sie schon immer wissen wollten aber nie zu Fragen trauten? Yahoo! Clever 
>> hilft Ihnen.
>>  [[alternative HTML version deleted]]
>>
>> __
>> R-help@stat.math.ethz.ch 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.
>>
> 
> __
> R-help@stat.math.ethz.ch 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.

__
R-help@stat.math.ethz.ch 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.


Re: [R] mirror vector?

2006-07-28 Thread Robin Hankin
Hi

Not that there's anything wrong with Jacques's answer, but
the List might be interested in the following gotcha:



 > m <- matrix(1:30,5,6)
 > apply(m,2,rev)
  [,1] [,2] [,3] [,4] [,5] [,6]
[1,]5   10   15   20   25   30
[2,]49   14   19   24   29
[3,]38   13   18   23   28
[4,]27   12   17   22   27
[5,]16   11   16   21   26
 > apply(m,1,rev)
  [,1] [,2] [,3] [,4] [,5]
[1,]   26   27   28   29   30
[2,]   21   22   23   24   25
[3,]   16   17   18   19   20
[4,]   11   12   13   14   15
[5,]6789   10
[6,]12345
 >

See how the first usage of apply() works as expected (at least for  
me ;-)
but the second returns the transpose of what one might need.
That's why I wrote arev().


comments anyone?





On 28 Jul 2006, at 08:56, Jacques VESLOT wrote:

>> mat <- matrix(1:16,4,4)
>> mat
>   [,1] [,2] [,3] [,4]
> [1,]159   13
> [2,]26   10   14
> [3,]37   11   15
> [4,]48   12   16
>> apply(mat,2,rev)
>   [,1] [,2] [,3] [,4]
> [1,]48   12   16
> [2,]37   11   15
> [3,]26   10   14
> [4,]159   13
>
> ---
> Jacques VESLOT
>
> CNRS UMR 8090
> I.B.L (2ème étage)
> 1 rue du Professeur Calmette
> B.P. 245
> 59019 Lille Cedex
>
> Tel : 33 (0)3.20.87.10.44
> Fax : 33 (0)3.20.87.10.31
>
> http://www-good.ibl.fr
> ---
>
>
> [EMAIL PROTECTED] a écrit :
>> Hello,
>>
>> I'm an absolut beginner with R and now I got a 2D vector with  
>> numbers. I would like to mirror this vector now by the rows (so  
>> that the first row becomes last, second becomes one before  
>> last, ...).
>> I don't know if there is any method I can use to do this.
>> Could you please help me?
>>
>> Antje
>>
>>  
>> -
>> Was Sie schon immer wissen wollten aber nie zu Fragen trauten?  
>> Yahoo! Clever hilft Ihnen.
>>  [[alternative HTML version deleted]]
>>
>> __
>> R-help@stat.math.ethz.ch 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.
>>
>
> __
> R-help@stat.math.ethz.ch 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.

--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743

__
R-help@stat.math.ethz.ch 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.


Re: [R] mirror vector?

2006-07-28 Thread Robin Hankin
Hi Antje


use the fact that n:1 counts backwards from n, and then use this as
a row index:

 > m <- matrix(1:30,5,6)
 > m[nrow(m):1,]
  [,1] [,2] [,3] [,4] [,5] [,6]
[1,]5   10   15   20   25   30
[2,]49   14   19   24   29
[3,]38   13   18   23   28
[4,]27   12   17   22   27
[5,]16   11   16   21   26


[
a more general answer would be

library(magic)
arev(m,1)
]



HTH

rksh



On 28 Jul 2006, at 08:50, <[EMAIL PROTECTED]> wrote:

> Hello,
>
> I'm an absolut beginner with R and now I got a 2D vector with  
> numbers. I would like to mirror this vector now by the rows (so  
> that the first row becomes last, second becomes one before last, ...).
> I don't know if there is any method I can use to do this.
> Could you please help me?
>
> Antje
>
>   
> -
> Was Sie schon immer wissen wollten aber nie zu Fragen trauten?  
> Yahoo! Clever hilft Ihnen.
>   [[alternative HTML version deleted]]
>
> __
> R-help@stat.math.ethz.ch 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.

--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743

__
R-help@stat.math.ethz.ch 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.


Re: [R] mirror vector?

2006-07-28 Thread Jacques VESLOT
 > mat <- matrix(1:16,4,4)
 > mat
  [,1] [,2] [,3] [,4]
[1,]159   13
[2,]26   10   14
[3,]37   11   15
[4,]48   12   16
 > apply(mat,2,rev)
  [,1] [,2] [,3] [,4]
[1,]48   12   16
[2,]37   11   15
[3,]26   10   14
[4,]159   13

---
Jacques VESLOT

CNRS UMR 8090
I.B.L (2ème étage)
1 rue du Professeur Calmette
B.P. 245
59019 Lille Cedex

Tel : 33 (0)3.20.87.10.44
Fax : 33 (0)3.20.87.10.31

http://www-good.ibl.fr
---


[EMAIL PROTECTED] a écrit :
> Hello,
> 
> I'm an absolut beginner with R and now I got a 2D vector with numbers. I 
> would like to mirror this vector now by the rows (so that the first row 
> becomes last, second becomes one before last, ...).
> I don't know if there is any method I can use to do this.
> Could you please help me?
> 
> Antje
> 
>   
> -
> Was Sie schon immer wissen wollten aber nie zu Fragen trauten? Yahoo! Clever 
> hilft Ihnen.
>   [[alternative HTML version deleted]]
> 
> __
> R-help@stat.math.ethz.ch 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.
>

__
R-help@stat.math.ethz.ch 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.