[R] Inverting data frame...row wise

Josip Dasovic jjd9 at sfu.ca
Thu Sep 25 23:53:27 CEST 2008


I think that Marko wanted his rows to be reversed, such that the first row becomes the last, while the second row becomes the second-to-last row, etc.

If I am assuming correctly, this will do what you want:

x<-data.frame(cbind(1:10, 1:10, 1:10, 1:10))
print(x)
   X1 X2 X3 X4
1   1  1  1  1
2   2  2  2  2
3   3  3  3  3
4   4  4  4  4
5   5  5  5  5
6   6  6  6  6
7   7  7  7  7
8   8  8  8  8
9   9  9  9  9
10 10 10 10 10

x1=x[order(nrow(x):1),] #invert row order
print(x1)

   X1 X2 X3 X4
10 10 10 10 10
9   9  9  9  9
8   8  8  8  8
7   7  7  7  7
6   6  6  6  6
5   5  5  5  5
4   4  4  4  4
3   3  3  3  3
2   2  2  2  2
1   1  1  1  1

Zivjeli!


----- Original Message -----
From: "Daniel Malter" <daniel at umd.edu>
To: "milicic.marko" <milicic.marko at gmail.com>, r-help at r-project.org
Sent: Thursday, September 25, 2008 2:37:16 PM GMT -08:00 US/Canada Pacific
Subject: Re: [R] Inverting data frame...row wise

So you want to make columns

1,2,3,4

into

4,3,2,1

or into

4,1,2,3?

The first option is done by:

x=c(rep(1,10),rep(2,10),rep(3,10),rep(4,10))
dim(x)=c(10,4) 
x=data.frame(x) #create data

x2=x[,order(-1:-4)] #invert column order

 


-------------------------
cuncta stricte discussurus
-------------------------

-----Ursprüngliche Nachricht-----
Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im
Auftrag von milicic.marko
Gesendet: Thursday, September 25, 2008 5:24 PM
An: r-help at r-project.org
Betreff: [R] Inverting data frame...row wise

Hi,

I have the data.frame with 4 columns. I simply want to invert dataset so
that last row becomes first...

I tried with rev(my_data-frame) but I got my columns inverted... not my rows


Thanks

______________________________________________
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.



More information about the R-help mailing list