[R] Inverting data frame...row wise

David Scott d.scott at auckland.ac.nz
Fri Sep 26 02:32:01 CEST 2008


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)
> df
   a  b
1 1  6
2 2  7
3 3  8
4 4  9
5 5 10
> revdf <- df[rev(rownames(df)),]
> revdf
   a  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



More information about the R-help mailing list