michael watson (IAH-C)
2006-Nov-28 10:45 UTC
[R] Sorting a data frame when you don't know the columns
Hi Sorry to ask such a well oiled question, but even with multiple google hits I don't think this has been answered very well. It's all well and good doing a sort of a data frame on multiple columns when you know in advance which columns you want to sort on, but what about when the names of the columns you wish to sort on are in a vector? At the minute I'm messing about with paste() to form a string that works for order() and then calling do.call(). Is this really the best way of doing it? Thanks Mick
Gabor Grothendieck
2006-Nov-28 10:55 UTC
[R] Sorting a data frame when you don't know the columns
DF <- data.frame(a = c(3, 4, 2, 3, 2, 4, 2), b = 7:1)) DF[do.call(order, DF),] will sort on all the columns. On 11/28/06, michael watson (IAH-C) <michael.watson at bbsrc.ac.uk> wrote:> Hi > > Sorry to ask such a well oiled question, but even with multiple google hits I don't think this has been answered very well. > > It's all well and good doing a sort of a data frame on multiple columns when you know in advance which columns you want to sort on, but what about when the names of the columns you wish to sort on are in a vector? > > At the minute I'm messing about with paste() to form a string that works for order() and then calling do.call(). Is this really the best way of doing it? > > Thanks > Mick > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >
Dimitris Rizopoulos
2006-Nov-28 11:03 UTC
[R] Sorting a data frame when you don't know the columns
try this: dat <- data.frame(x = rnorm(100), y = sample(rep(1:2, each = 50)), z = sample(rep(1:4, 25))) ######### nams <- c("y", "z") dat[do.call(order, dat[nams]), ] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "michael watson (IAH-C)" <michael.watson at bbsrc.ac.uk> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, November 28, 2006 11:45 AM Subject: [R] Sorting a data frame when you don't know the columns> Hi > > Sorry to ask such a well oiled question, but even with multiple > google hits I don't think this has been answered very well. > > It's all well and good doing a sort of a data frame on multiple > columns when you know in advance which columns you want to sort on, > but what about when the names of the columns you wish to sort on are > in a vector? > > At the minute I'm messing about with paste() to form a string that > works for order() and then calling do.call(). Is this really the > best way of doing it? > > Thanks > Mick > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm