Hi, I want to sort a matrix by a specific variable without changing the row binding between variables. Ex. Name Sex Age Fred M 24 John M 18 Mary F 21 ordered by Age Name Sex Age John M 18 Mary F 21 Fred M 24 Thanks Angelo
Dear Angelo,
What about this?
mydata=read.table(textConnection("Name Sex Age
Fred M 24
John M 18
Mary F 21"), header=TRUE,sep="")
mydata[order(mydata$Age),]
HTH,
Jorge
On Thu, Jul 10, 2008 at 2:21 PM, Angelo Scozzarella <
angeloscozzarella@tiscali.it> wrote:
> Hi,
>
> I want to sort a matrix by a specific variable without changing the row
> binding between variables.
>
> Ex.
>
> Name Sex Age
> Fred M 24
> John M 18
> Mary F 21
>
>
> ordered by Age
>
>
> Name Sex Age
> John M 18
> Mary F 21
> Fred M 24
>
>
>
> Thanks
>
>
>
> Angelo
>
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]
Do you really want to sort a *matrix*, or do you mean a data.frame? See ?order. Angelo Scozzarella wrote:> Hi, > > I want to sort a matrix by a specific variable without changing the row > binding between variables. > > Ex. > > Name Sex Age > Fred M 24 > John M 18 > Mary F 21 > > > ordered by Age > > > Name Sex Age > John M 18 > Mary F 21 > Fred M 24 > > > > Thanks > > > > Angelo > > ______________________________________________ > 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.
1. This is a data frame, not a matrix ! -- ?data.frame
2. help.search("sort") is the first thing you should have done. You
probably
wouldn't have needed to post if you had.
3. The canonical answer is ?order and indexing -- as in:
yourdf[order(yourdf$Age),]
Note that order() can be used to sort by more than one column -- e.g. Sex
and then Age within Sex.
4. The orderBy() function in the doBy package -- and probably there are
others -- can do this exactly.
Cheers,
Bert Gunter
Genentech, Inc.
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
Behalf Of Angelo Scozzarella
Sent: Thursday, July 10, 2008 11:21 AM
To: r-help at r-project.org
Subject: [R] Sorting a matrix
Hi,
I want to sort a matrix by a specific variable without changing the
row binding between variables.
Ex.
Name Sex Age
Fred M 24
John M 18
Mary F 21
ordered by Age
Name Sex Age
John M 18
Mary F 21
Fred M 24
Thanks
Angelo
______________________________________________
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.