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
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.
Sorry, I misread. I thought you want to get the columns inverted. It works for rows analogously: x=c(rep(1:10,4)) dim(x)=c(10,4) x=data.frame(x) x2=x[order(-1:-10),] x2 ------------------------- 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.
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.
How about something like my.data=my.data[,4:1] Julian milicic.marko wrote:> 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.
On Thu, 25 Sep 2008, Julian Burgos wrote:> How about something like > > my.data=my.data[,4:1] > > Julian >This can be tackled in a similar way to the question by Mark Na which I just answered. Using the same data frame construction we have> df <- data.frame(k1=1:2,k2=3:4,z=5:6,a=7:8,y=9:10) > dfk1 k2 z a y 1 1 3 5 7 9 2 2 4 6 8 10> revdf <- df[,rev(names(df))] > revdfy a z k2 k1 1 9 7 5 3 1 2 10 8 6 4 2 David Scott> milicic.marko wrote: >> 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. >_________________________________________________________________ David Scott Department of Statistics, Tamaki Campus The University of Auckland, PB 92019 Auckland 1142, NEW ZEALAND Phone: +64 9 373 7599 ext 86830 Fax: +64 9 373 7000 Email: d.scott at auckland.ac.nz Graduate Officer, Department of Statistics Director of Consulting, Department of Statistics
On Fri, 26 Sep 2008, David Scott wrote:> On Thu, 25 Sep 2008, Julian Burgos wrote: > >> How about something like >> >> my.data=my.data[,4:1] >> >> Julian >> > > This can be tackled in a similar way to the question by Mark Na which I just > answered. Using the same data frame construction we have > >> df <- data.frame(k1=1:2,k2=3:4,z=5:6,a=7:8,y=9:10) >> df > k1 k2 z a y > 1 1 3 5 7 9 > 2 2 4 6 8 10 >> revdf <- df[,rev(names(df))] >> revdf > y a z k2 k1 > 1 9 7 5 3 1 > 2 10 8 6 4 2 > >Oh dear, read the question again! Reverse the order of the *rows*:> df <- data.frame(a=1:5,b=6:10) > dfa b 1 1 6 2 2 7 3 3 8 4 4 9 5 5 10> revdf <- df[rev(rownames(df)),] > revdfa b 5 5 10 4 4 9 3 3 8 2 2 7 1 1 6 David Scott> > >> milicic.marko wrote: >>> 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. >> > > _________________________________________________________________ > David Scott Department of Statistics, Tamaki Campus > The University of Auckland, PB 92019 > Auckland 1142, NEW ZEALAND > Phone: +64 9 373 7599 ext 86830 Fax: +64 9 373 7000 > Email: d.scott at auckland.ac.nz > > Graduate Officer, Department of Statistics > Director of Consulting, Department of Statistics > > ______________________________________________ > 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. >_________________________________________________________________ David Scott Department of Statistics, Tamaki Campus The University of Auckland, PB 92019 Auckland 1142, NEW ZEALAND Phone: +64 9 373 7599 ext 86830 Fax: +64 9 373 7000 Email: d.scott at auckland.ac.nz Graduate Officer, Department of Statistics Director of Consulting, Department of Statistics