[R] Sort a data frame with respect to more than one variable
Dear All, How can I sort a data frame with respect to more than one variable? I know that for one variable X one may use: df[order(df$X), ] where df is the data frame containing X. Many thanks, Bernard - [[alternative HTML version deleted]] __ R-help@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
Re: [R] Sort a data frame with respect to more than one variable
Le 21.09.2005 10:54, Marc Bernard a écrit : Dear All, How can I sort a data frame with respect to more than one variable? I know that for one variable X one may use: df[order(df$X), ] where df is the data frame containing X. Many thanks, Bernard You already know the answer of that question. order() accepts more than one variable. x - rep(rbinom(5,size=4,prob=.5), 6) y - rnorm(30) cbind(x,y)[ order(x,y) , ] Romain -- visit the R Graph Gallery : http://addictedtor.free.fr/graphiques ~ ~~ Romain FRANCOIS - http://addictedtor.free.fr ~~ Etudiant ISUP - CS3 - Industrie et Services ~~http://www.isup.cicrp.jussieu.fr/ ~~ Stagiaire INRIA Futurs - Equipe SELECT ~~ http://www.inria.fr/recherche/equipes/select.fr.html~~ ~ __ R-help@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
Re: [R] Sort a data frame with respect to more than one variable
if you need nested sorting then you can still use order(), e.g., in a longitudinal setting you may want to sort your data.frame with respect to subject's id and time within subject then use, df[order(df$id, df$time), ] 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/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm - Original Message - From: Marc Bernard [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent: Wednesday, September 21, 2005 10:54 AM Subject: [R] Sort a data frame with respect to more than one variable Dear All, How can I sort a data frame with respect to more than one variable? I know that for one variable X one may use: df[order(df$X), ] where df is the data frame containing X. Many thanks, Bernard - [[alternative HTML version deleted]] __ R-help@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 Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ R-help@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
Re: [R] Sort a data frame with respect to more than one variable
Marc Bernard a écrit : I know that for one variable X one may use: df[order(df$X), ] where df is the data frame containing X. probably not optimal, but simply why not ? dfX = df[order(df$X), ]; dfXY = dfX[order(dfX$Y), ]; hih __ R-help@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
[R] Sort a data frame
Hi all I have the next data frame year STODSLAGNR TAL TALT TALVEKT 1 2002 2120006 57 1 NA 1 2 1997 9703003257 NA NA NA 3 1997 9703007127 1 NA NA 4 1997 9703000557 1 NA NA 5 1997 9702012760 NA 1 NA 6 2001 1160025 27 1 NA 1 7 1998 9802006960 1 NA NA 8 1996 9603000957 NA 1 NA How to sort it according to year column Sort does seem to work only on vectors Thank you Luis Ridao Cruz Fiskirannsóknarstovan Nóatún 1 P.O. Box 3051 FR-110 Tórshavn Faroe Islands Phone: +298 353900 Phone(direct): +298 353912 Mobile: +298 580800 Fax: +298 353901 E-mail: [EMAIL PROTECTED] Web:www.frs.fo __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Sort a data frame
Hi Luis, you could try something like, data[order(data$year),] I hope this helps. Best, Dimitris Dimitris Rizopoulos Doctoral Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm - Original Message - From: Luis Rideau Cruz [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Tuesday, July 20, 2004 10:51 AM Subject: [R] Sort a data frame Hi all I have the next data frame year STODSLAGNR TAL TALT TALVEKT 1 2002 2120006 57 1 NA 1 2 1997 9703003257 NA NA NA 3 1997 9703007127 1 NA NA 4 1997 9703000557 1 NA NA 5 1997 9702012760 NA 1 NA 6 2001 1160025 27 1 NA 1 7 1998 9802006960 1 NA NA 8 1996 9603000957 NA 1 NA How to sort it according to year column Sort does seem to work only on vectors Thank you Luis Ridao Cruz Fiskirannsóknarstovan Nóatún 1 P.O. Box 3051 FR-110 Tórshavn Faroe Islands Phone: +298 353900 Phone(direct): +298 353912 Mobile: +298 580800 Fax: +298 353901 E-mail: [EMAIL PROTECTED] Web:www.frs.fo __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Sort a data frame
Hi Lets assign your data frame to the variable yourdf then: yourdf[ , order( yourdf$year ) ] should sort it. For decreasing increasing sorting see ?order Sincerely Eryk. *** REPLY SEPARATOR *** On 20.07.2004 at 09:51 Luis Rideau Cruz wrote: Hi all I have the next data frame year STODSLAGNR TAL TALT TALVEKT 1 2002 2120006 57 1 NA 1 2 1997 9703003257 NA NA NA 3 1997 9703007127 1 NA NA 4 1997 9703000557 1 NA NA 5 1997 9702012760 NA 1 NA 6 2001 1160025 27 1 NA 1 7 1998 9802006960 1 NA NA 8 1996 9603000957 NA 1 NA How to sort it according to year column Sort does seem to work only on vectors Thank you Luis Ridao Cruz Fiskirannsóknarstovan Nóatún 1 P.O. Box 3051 FR-110 Tórshavn Faroe Islands Phone: +298 353900 Phone(direct): +298 353912 Mobile: +298 580800 Fax: +298 353901 E-mail: [EMAIL PROTECTED] Web:www.frs.fo __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Sort a data frame
Try to convert the dataframe in a matrix: z-as.matrix(yourdataframe) sort(z) It would run! Ciao Vito Hi all I have the next data frame year STODSLAGNR TAL TALT TALVEKT 1 2002 2120006 57 1 NA 1 2 1997 9703003257 NA NA NA 3 1997 9703007127 1 NA NA 4 1997 9703000557 1 NA NA 5 1997 9702012760 NA 1 NA 6 2001 1160025 27 1 NA 1 7 1998 9802006960 1 NA NA 8 1996 9603000957 NA 1 NA How to sort it according to year column Sort does seem to work only on vectors Thank you Luis Ridao Cruz Fiskirannsóknarstovan Nóatún 1 P.O. Box 3051 FR-110 Tórshavn Faroe Islands Phone: +298 353900 Phone(direct): +298 353912 Mobile: +298 580800 Fax: +298 353901 E-mail: luisr at frs.fo Web:www.frs.fo = Diventare costruttori di soluzioni Visitate il portale http://www.modugno.it/ e in particolare la sezione su Palese http://www.modugno.it/archivio/cat_palese.shtml __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Sort a data frame
hi, i had this trouble a while ago and found this function in latest R-help. SortMat - function(Mat, sort) { m - do.call(order, as.data.frame(Mat[, Sort])) Mat[m, ] } where mat is a matrix and sort the column you want to be sorted. convert you dataframe into matrix or change this function to be used with dataframe. Best, Le mar 20/07/2004 à 10:51, Luis Rideau Cruz a écrit : Hi all I have the next data frame year STODSLAGNR TAL TALT TALVEKT 1 2002 2120006 57 1 NA 1 2 1997 9703003257 NA NA NA 3 1997 9703007127 1 NA NA 4 1997 9703000557 1 NA NA 5 1997 9702012760 NA 1 NA 6 2001 1160025 27 1 NA 1 7 1998 9802006960 1 NA NA 8 1996 9603000957 NA 1 NA How to sort it according to year column Sort does seem to work only on vectors Thank you Luis Ridao Cruz Fiskirannsóknarstovan Nóatún 1 P.O. Box 3051 FR-110 Tórshavn Faroe Islands Phone: +298 353900 Phone(direct): +298 353912 Mobile: +298 580800 Fax: +298 353901 E-mail: [EMAIL PROTECTED] Web:www.frs.fo __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- -- Yves Magliulo [EMAIL PROTECTED] Climatology research department, CLIMPACT Tel. : +33 (0) 1 55 07 85 77 Fax. : +33 (0) 1 55 07 85 79 Universite Pierre et Marie Curie Boite 101 - Tour 45 - 5eme etage - Couloir 45/46 4 place Jussieu, 75252 Paris CEDEX 05, France __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Sort a data frame
Hallo or you can your.df[order(your.df$year),] Cheers Petr On 20 Jul 2004 at 14:35, Yves Magliulo wrote: hi, i had this trouble a while ago and found this function in latest R-help. SortMat - function(Mat, sort) { m - do.call(order, as.data.frame(Mat[, Sort])) Mat[m, ] } where mat is a matrix and sort the column you want to be sorted. convert you dataframe into matrix or change this function to be used with dataframe. Best, Le mar 20/07/2004 10:51, Luis Rideau Cruz a crit : Hi all I have the next data frame year STODSLAGNR TAL TALT TALVEKT 1 2002 2120006 57 1 NA 1 2 1997 9703003257 NA NA NA 3 1997 9703007127 1 NA NA 4 1997 9703000557 1 NA NA 5 1997 9702012760 NA 1 NA 6 2001 1160025 27 1 NA 1 7 1998 9802006960 1 NA NA 8 1996 9603000957 NA 1 NA How to sort it according to year column Sort does seem to work only on vectors Thank you Luis Ridao Cruz Fiskirannsknarstovan Natn 1 P.O. Box 3051 FR-110 Trshavn Faroe Islands Phone: +298 353900 Phone(direct): +298 353912 Mobile: +298 580800 Fax: +298 353901 E-mail: [EMAIL PROTECTED] Web:www.frs.fo __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- -- Yves Magliulo [EMAIL PROTECTED] Climatology research department, CLIMPACT Tel. : +33 (0) 1 55 07 85 77 Fax. : +33 (0) 1 55 07 85 79 Universite Pierre et Marie Curie Boite 101 - Tour 45 - 5eme etage - Couloir 45/46 4 place Jussieu, 75252 Paris CEDEX 05, France __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Petr Pikal [EMAIL PROTECTED] __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] Sort a data frame
Since this is such an FAQ, perhaps sort() could be made generic, with a method for data frame? Here's a crack at it: sort.data.frame - function(x, key, ...) { if (missing(key)) { rn - rownames(x) if (all(rn %in% 1:nrow(x))) rn - as.numeric(rn) x[order(rn, ...), , drop=FALSE] } else { x[do.call(order, c(x[key], ...)), , drop=FALSE] } } Here the default behavior, when `key' is not supplied, is to sort by rownames. One (more sensible?) alternative is to sort by all columns in the data frame, by making key=names(x). [If the rownames of the data frame are numbers but not consecutive integers, it may give the unintuitive result. Does anyone know how to test whether a character can be coerced into numeric without generating a warning?] Andy From: Luis Rideau Cruz Hi all I have the next data frame year STODSLAGNR TAL TALT TALVEKT 1 2002 2120006 57 1 NA 1 2 1997 9703003257 NA NA NA 3 1997 9703007127 1 NA NA 4 1997 9703000557 1 NA NA 5 1997 9702012760 NA 1 NA 6 2001 1160025 27 1 NA 1 7 1998 9802006960 1 NA NA 8 1996 9603000957 NA 1 NA How to sort it according to year column Sort does seem to work only on vectors Thank you Luis Ridao Cruz Fiskirannsóknarstovan Nóatún 1 P.O. Box 3051 FR-110 Tórshavn Faroe Islands Phone: +298 353900 Phone(direct): +298 353912 Mobile: +298 580800 Fax: +298 353901 E-mail: [EMAIL PROTECTED] Web:www.frs.fo __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Sort a data frame
Liaw, Andy andy_liaw at merck.com writes: Does anyone know how to test whether a character can be coerced into numeric without generating a warning? res - tryCatch(as.numeric(x), warning = function(x)x) res is now numeric if x was successfully converted and is unchanged if it was not. You can test res using is.numeric and take appropriate action or you can put the action into the body of the warning function such as having it return 0. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Sort a data frame
Gabor Grothendieck ggrothendieck at myway.com writes: Liaw, Andy andy_liaw at merck.com writes: Does anyone know how to test whether a character can be coerced into numeric without generating a warning? res - tryCatch(as.numeric(x), warning = function(x)x) res is now numeric if x was successfully converted and is unchanged if it was not. You can test res using is.numeric and take appropriate action or you can put the action into the body of the warning function such as having it return 0. Sorry. The above has an error. It should be: # res is x converted to numeric or x without change if that fails res - tryCatch(as.numeric(x), warning = function(warn)x) or # res is x converted to numeric or 0 if that fails res - tryCatch(as.numeric(x), warning = function(warn)0) __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Sort a data frame
Gabor Grothendieck wrote: Gabor Grothendieck ggrothendieck at myway.com writes: Liaw, Andy andy_liaw at merck.com writes: Does anyone know how to test whether a character can be coerced into numeric without generating a warning? res - tryCatch(as.numeric(x), warning = function(x)x) res is now numeric if x was successfully converted and is unchanged if it was not. You can test res using is.numeric and take appropriate action or you can put the action into the body of the warning function such as having it return 0. Also: library(Hmisc) example(all.is.numeric) Kjetil Halvorsen Sorry. The above has an error. It should be: # res is x converted to numeric or x without change if that fails res - tryCatch(as.numeric(x), warning = function(warn)x) or # res is x converted to numeric or 0 if that fails res - tryCatch(as.numeric(x), warning = function(warn)0) __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html