Briggs, Meredith M
2005-Jul-08 00:08 UTC
[R] How do you sort a data frame on a selection of columns?
This is what to start with: Data Frame A B C D c1 4 y 5 c3 6 d 7 c1 5 t 6 Now sort on A then C This is what to end with: Data Frame A B C D c1 5 t 6 c1 4 y 5 c3 6 d 7 I assume it is something like this: attach(DF) sort(DF,partial=c(A,C)) Thanks in advance. Meredith [[alternative HTML version deleted]]
Peter Dalgaard
2005-Jul-08 00:21 UTC
[R] How do you sort a data frame on a selection of columns?
"Briggs, Meredith M" <Meredith.Briggs at team.telstra.com> writes:> This is what to start with: > > Data Frame A B C D > c1 4 y 5 > c3 6 d 7 > c1 5 t 6 > > Now sort on A then C > > This is what to end with: > > Data Frame A B C D > c1 5 t 6 > c1 4 y 5 > c3 6 d 7 > > I assume it is something like this: > > attach(DF)Attaching data frames before modifying them is not usually a good idea. Especially if you forget to detach them again.> sort(DF,partial=c(A,C))o <- with(DF, order(A,C)) # or just order(DF$A, DF$C) DF <- DF[o,] -- O__ ---- Peter Dalgaard ??ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Gabor Grothendieck
2005-Jul-08 00:22 UTC
[R] How do you sort a data frame on a selection of columns?
On 7/7/05, Briggs, Meredith M <Meredith.Briggs at team.telstra.com> wrote:> This is what to start with: > > Data Frame A B C D > c1 4 y 5 > c3 6 d 7 > c1 5 t 6 > > Now sort on A then C > > This is what to end with: > > Data Frame A B C D > c1 5 t 6 > c1 4 y 5 > c3 6 d 7 >DF[order(DF$A, DF$C),] There is also a function posted on r-help that can do this easier. Try RSiteSearch("sort.data.frame")
Spencer Graves
2005-Jul-08 00:26 UTC
[R] How do you sort a data frame on a selection of columns?
Does "order" do what you want? spencer graves Briggs, Meredith M wrote:> This is what to start with: > > Data Frame A B C D > c1 4 y 5 > c3 6 d 7 > c1 5 t 6 > > Now sort on A then C > > This is what to end with: > > Data Frame A B C D > c1 5 t 6 > c1 4 y 5 > c3 6 d 7 > > I assume it is something like this: > > attach(DF) > sort(DF,partial=c(A,C)) > > > > Thanks in advance. > > Meredith > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- Spencer Graves, PhD Senior Development Engineer PDF Solutions, Inc. 333 West San Carlos Street Suite 700 San Jose, CA 95110, USA spencer.graves at pdf.com www.pdf.com <http://www.pdf.com> Tel: 408-938-4420 Fax: 408-280-7915