[R] sorting a data frame by rownames

From: Michael Rennie <mdrennie_at_gmail.com>
Date: Tue, 08 Jul 2008 22:58:34 -0400


Hi there,

I'm sure there's an easy answer to this, and I can't wait to see it.

The question: is there an easy way to sort a data frame by it's row names?

My dilemma:

I've had to pull apart a data frame, run it through a loop to do some calculations and generate new variables, and then re-construct the chunks back into a data frame at the end.

Doing this preserves the row names from the original data frame (which are informative), so i thought this would make it easy to bring it back to it's original order.

However, to do this, I've had to include the row names as a column, and then sort by that at the end, which seems terribly redundant and makes me think there's an easier way to do this.

See some example code below:

###########################
#build a dataframe

set.seed(12345)

a<-sample(1:50, 15)
b<-15:1
c<-rep(1:3, 5)

ex.dat<-data.frame(a,b,c)

#pull it apart; in my case, each chunk is going through a loop and being
spit out in a list

sub3<-subset(ex.dat, c=="3")
sub2<-subset(ex.dat, c=="2")
sub1<-subset(ex.dat, c=="1")

#put it back together; in my case, pull out the parts of the list generated
by the loop that hold the data

newdat<-rbind(sub3, sub2, sub1)

#rebuild it such that it can be re-organized into it's original order

rn<-as.numeric(row.names(newdat))

new2<-data.frame(newdat, rn)

new3 = new2[do.call(order, new2["rn"]), ]

######################

It's those last three lines of code that I'm sure are unnecessary; why include a column of information for something that's already there? However, most of the posted solutions to sorting of data frames have to do with sorting by particular variables, not by the row names (which are just typically 1:n, and is rarely informative). So, this is the solution I came up with based on what I can find out there currently.

Looking forward to any thoughts or suggestions.

--
Michael D. Rennie
Ph.D. Candidate
University of Toronto at Mississauga
3359 Missisagua Rd. N.
Mississauga, ON L5L 1C6
Ph: 905-828-5452 Fax: 905-828-3792
www.utm.utoronto.ca/~w3rennie

	[[alternative HTML version deleted]]

______________________________________________
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.
Received on Wed 09 Jul 2008 - 03:05:09 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Jul 2008 - 04:31:40 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive