-2

im trying to change the row order of a DataFrame , so the last digit is in the first place, the penultimate in the second place and so on.

it looks like this

x1=1:2
x2=1:5
x3=c(0,1,2,0)
n=max(length(x1), length(x2), length(x3))
length(x1)=n                      
length(x2)=n
length(x3)=n
a=rbind(x1,x2,x3)
a

#What im trying to do is that every row is like:

b=rev(a) #Keeping the NA where they are

Thanks!

CrisGuN
  • 19
  • 7
  • 2
    `?rev` will do this for vectors ("row" does not make sense for a vector). For matrices or data.frames (objects with rows), you could use `df[rev(seq_len(nrow(df))),]` for an object named df. – lmo Feb 28 '17 at 13:21
  • its thought for a dataframe, thank you for your answer! – CrisGuN Feb 28 '17 at 13:26

3 Answers3

0

You can use rev function: rev(x1)

Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107
0

You want to use rev.

> reverse <- rev(x1)
[1] 0 1 0 0 1 2 0 0 1

From its documentation:

rev provides a reversed version of its argument

basbabybel
  • 780
  • 8
  • 17
0

As @Imo wrote:

x2 <-rev(x1)
staove7
  • 560
  • 5
  • 18