Hi I have a vector x <- c(1, 2, 3, 4, 5) and I want to "flip" it around, i.e. I need 5, 4, 3, 2, 1 Is there a ssolution apart from y <- x[length(x):1] I am also looking for the same for a matrix M, i.e. 1 2 3 4 5 6 7 8 9 should become 7 8 9 4 5 6 1 2 3 again, I am using M[1:dim(M)[1], dim(M)[2]:1] Thanks Rainer
One way:> x<-1:9 > rev(x)[1] 9 8 7 6 5 4 3 2 1 for x as the matrix you gave:> x[,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9> > > apply(t(x),1,rev)[,1] [,2] [,3] [1,] 7 8 9 [2,] 4 5 6 [3,] 1 2 3 On Tue, 23 Oct 2007, Rainer M Krug wrote:> Hi > > I have a vector > > x <- c(1, 2, 3, 4, 5) > > and I want to "flip" it around, i.e. I need > > 5, 4, 3, 2, 1 > > Is there a ssolution apart from > > y <- x[length(x):1] > > > I am also looking for the same for a matrix M, i.e. > > 1 2 3 > 4 5 6 > 7 8 9 > > should become > > 7 8 9 > 4 5 6 > 1 2 3 > > again, I am using > > M[1:dim(M)[1], dim(M)[2]:1] > > > Thanks > > Rainer > > ______________________________________________ > 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. >
Thanks Katharine that answers my vector question. The suggestion for the matrix is interesting and I would have never thought about theat. Thanks Rainer Katharine Mullen wrote:> One way: > >> x<-1:9 >> rev(x) > [1] 9 8 7 6 5 4 3 2 1 > > for x as the matrix you gave: >> x > [,1] [,2] [,3] > [1,] 1 2 3 > [2,] 4 5 6 > [3,] 7 8 9 >> >> apply(t(x),1,rev) > [,1] [,2] [,3] > [1,] 7 8 9 > [2,] 4 5 6 > [3,] 1 2 3 > > > On Tue, 23 Oct 2007, Rainer M Krug wrote: > >> Hi >> >> I have a vector >> >> x <- c(1, 2, 3, 4, 5) >> >> and I want to "flip" it around, i.e. I need >> >> 5, 4, 3, 2, 1 >> >> Is there a ssolution apart from >> >> y <- x[length(x):1] >> >> >> I am also looking for the same for a matrix M, i.e. >> >> 1 2 3 >> 4 5 6 >> 7 8 9 >> >> should become >> >> 7 8 9 >> 4 5 6 >> 1 2 3 >> >> again, I am using >> >> M[1:dim(M)[1], dim(M)[2]:1] >> >> >> Thanks >> >> Rainer >> >> ______________________________________________ >> 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. >>
sorry, just do rev on the columns (no t()):> x[,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9> apply(x,2,rev)[,1] [,2] [,3] [1,] 7 8 9 [2,] 4 5 6 [3,] 1 2 3 On Tue, 23 Oct 2007, Katharine Mullen wrote:> One way: > > > x<-1:9 > > rev(x) > [1] 9 8 7 6 5 4 3 2 1 > > for x as the matrix you gave: > > x > [,1] [,2] [,3] > [1,] 1 2 3 > [2,] 4 5 6 > [3,] 7 8 9 > > > > > > apply(t(x),1,rev) > [,1] [,2] [,3] > [1,] 7 8 9 > [2,] 4 5 6 > [3,] 1 2 3 > > > On Tue, 23 Oct 2007, Rainer M Krug wrote: > > > Hi > > > > I have a vector > > > > x <- c(1, 2, 3, 4, 5) > > > > and I want to "flip" it around, i.e. I need > > > > 5, 4, 3, 2, 1 > > > > Is there a ssolution apart from > > > > y <- x[length(x):1] > > > > > > I am also looking for the same for a matrix M, i.e. > > > > 1 2 3 > > 4 5 6 > > 7 8 9 > > > > should become > > > > 7 8 9 > > 4 5 6 > > 1 2 3 > > > > again, I am using > > > > M[1:dim(M)[1], dim(M)[2]:1] > > > > > > Thanks > > > > Rainer > > > > ______________________________________________ > > 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. > > > > ______________________________________________ > 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. >
Hello
for vectors, use rev()
For matrices and arbitrary-dimensioned arrays,
use arev() of the magic package:
> library(magic)
> b <- matrix(1:9,3,3)
> b
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> arev(b)
[,1] [,2] [,3]
[1,] 9 6 3
[2,] 8 5 2
[3,] 7 4 1
>
On 23 Oct 2007, at 11:45, Rainer M Krug wrote:
> Hi
>
> I have a vector
>
> x <- c(1, 2, 3, 4, 5)
>
> and I want to "flip" it around, i.e. I need
>
> 5, 4, 3, 2, 1
>
> Is there a ssolution apart from
>
> y <- x[length(x):1]
>
>
> I am also looking for the same for a matrix M, i.e.
>
> 1 2 3
> 4 5 6
> 7 8 9
>
> should become
>
> 7 8 9
> 4 5 6
> 1 2 3
>
> again, I am using
>
> M[1:dim(M)[1], dim(M)[2]:1]
>
>
> Thanks
>
> Rainer
>
> ______________________________________________
> 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.
--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
tel 023-8059-7743
Sorry misread the question.
Function arev() takes a second argument that specifies
which dimensions to flip:
> b <- matrix(1:9,3,3,byrow=T)
> b
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> arev(b,1)
[,1] [,2] [,3]
[1,] 7 8 9
[2,] 4 5 6
[3,] 1 2 3
>
On 23 Oct 2007, at 11:45, Rainer M Krug wrote:
> Hi
>
> I have a vector
>
> x <- c(1, 2, 3, 4, 5)
>
> and I want to "flip" it around, i.e. I need
>
> 5, 4, 3, 2, 1
>
> Is there a ssolution apart from
>
> y <- x[length(x):1]
>
>
> I am also looking for the same for a matrix M, i.e.
>
> 1 2 3
> 4 5 6
> 7 8 9
>
> should become
>
> 7 8 9
> 4 5 6
> 1 2 3
>
> again, I am using
>
> M[1:dim(M)[1], dim(M)[2]:1]
>
>
> Thanks
>
> Rainer
>
> ______________________________________________
> 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.
--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
tel 023-8059-7743
Hi Robin I knew that something like arev was somewhere around. Thanks Rainer Robin Hankin wrote:> Hello > > > for vectors, use rev() > > For matrices and arbitrary-dimensioned arrays, > use arev() of the magic package: > > > > library(magic) > > b <- matrix(1:9,3,3) > > b > [,1] [,2] [,3] > [1,] 1 4 7 > [2,] 2 5 8 > [3,] 3 6 9 > > arev(b) > [,1] [,2] [,3] > [1,] 9 6 3 > [2,] 8 5 2 > [3,] 7 4 1 > > > > > > > > > On 23 Oct 2007, at 11:45, Rainer M Krug wrote: > >> Hi >> >> I have a vector >> >> x <- c(1, 2, 3, 4, 5) >> >> and I want to "flip" it around, i.e. I need >> >> 5, 4, 3, 2, 1 >> >> Is there a ssolution apart from >> >> y <- x[length(x):1] >> >> >> I am also looking for the same for a matrix M, i.e. >> >> 1 2 3 >> 4 5 6 >> 7 8 9 >> >> should become >> >> 7 8 9 >> 4 5 6 >> 1 2 3 >> >> again, I am using >> >> M[1:dim(M)[1], dim(M)[2]:1] >> >> >> Thanks >> >> Rainer >> >> ______________________________________________ >> 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. > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 >