Hi all I have the next data frame year STOD SLAGNR TAL TALT TALVEKT 1 2002 2120006 57 1 NA 1 2 1997 97030032 57 NA NA NA 3 1997 97030071 27 1 NA NA 4 1997 97030005 57 1 NA NA 5 1997 97020127 60 NA 1 NA 6 2001 1160025 27 1 NA 1 7 1998 98020069 60 1 NA NA 8 1996 96030009 57 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
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" <Luisr at frs.fo> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, July 20, 2004 10:51 AM Subject: [R] Sort a data frame> Hi all > > I have the next data frame > > year STOD SLAGNR TAL TALT TALVEKT > 1 2002 2120006 57 1 NA 1 > 2 1997 97030032 57 NA NA NA > 3 1997 97030071 27 1 NA NA > 4 1997 97030005 57 1 NA NA > 5 1997 97020127 60 NA 1 NA > 6 2001 1160025 27 1 NA 1 > 7 1998 98020069 60 1 NA NA > 8 1996 96030009 57 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 > > ______________________________________________ > R-help at stat.math.ethz.ch 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
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 STOD SLAGNR TAL TALT TALVEKT >1 2002 2120006 57 1 NA 1 >2 1997 97030032 57 NA NA NA >3 1997 97030071 27 1 NA NA >4 1997 97030005 57 1 NA NA >5 1997 97020127 60 NA 1 NA >6 2001 1160025 27 1 NA 1 >7 1998 98020069 60 1 NA NA >8 1996 96030009 57 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 > >______________________________________________ >R-help at stat.math.ethz.ch 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
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 STOD SLAGNR TAL TALT TALVEKT 1 2002 2120006 57 1 NA 1 2 1997 97030032 57 NA NA NA 3 1997 97030071 27 1 NA NA 4 1997 97030005 57 1 NA NA 5 1997 97020127 60 NA 1 NA 6 2001 1160025 27 1 NA 1 7 1998 98020069 60 1 NA NA 8 1996 96030009 57 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
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 STOD SLAGNR TAL TALT TALVEKT > 1 2002 2120006 57 1 NA 1 > 2 1997 97030032 57 NA NA NA > 3 1997 97030071 27 1 NA NA > 4 1997 97030005 57 1 NA NA > 5 1997 97020127 60 NA 1 NA > 6 2001 1160025 27 1 NA 1 > 7 1998 98020069 60 1 NA NA > 8 1996 96030009 57 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 > > ______________________________________________ > R-help at stat.math.ethz.ch 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 <ym at climpact.com> 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
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 STOD SLAGNR TAL TALT TALVEKT > > 1 2002 2120006 57 1 NA 1 > > 2 1997 97030032 57 NA NA NA > > 3 1997 97030071 27 1 NA NA > > 4 1997 97030005 57 1 NA NA > > 5 1997 97020127 60 NA 1 NA > > 6 2001 1160025 27 1 NA 1 > > 7 1998 98020069 60 1 NA NA > > 8 1996 96030009 57 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 > > > > ______________________________________________ > > R-help at stat.math.ethz.ch 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 <ym at climpact.com> > 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 > > ______________________________________________ > R-help at stat.math.ethz.ch 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.htmlPetr Pikal petr.pikal at precheza.cz
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 STOD SLAGNR TAL TALT TALVEKT > 1 2002 2120006 57 1 NA 1 > 2 1997 97030032 57 NA NA NA > 3 1997 97030071 27 1 NA NA > 4 1997 97030005 57 1 NA NA > 5 1997 97020127 60 NA 1 NA > 6 2001 1160025 27 1 NA 1 > 7 1998 98020069 60 1 NA NA > 8 1996 96030009 57 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 > > ______________________________________________ > R-help at stat.math.ethz.ch 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 > >
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.
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)
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) > >______________________________________________ >R-help at stat.math.ethz.ch 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 > > > >