I have following object :> date2[,1] [,2] [1,] "apr" "1992" [2,] "aug" "1992" [3,] "dec" "1992" [4,] "feb" "1992" [5,] "jan" "1992" [6,] "jul" "1992" [7,] "jun" "1992" [8,] "mar" "1992" [9,] "may" "1992" [10,] "nov" "1992" [11,] "oct" "1992" [12,] "sep" "1992" [13,] "apr" "1993" [14,] "aug" "1993" [15,] "dec" "1993" [16,] "feb" "1993" [17,] "jan" "1993" [18,] "jul" "1993" [19,] "jun" "1993" [20,] "mar" "1993" [21,] "may" "1993" [22,] "nov" "1993" [23,] "oct" "1993" [24,] "sep" "1993" [25,] "apr" "1994" [26,] "aug" "1994" [27,] "dec" "1994" [28,] "feb" "1994" [29,] "jan" "1994" [30,] "jul" "1994" Now I want to sort the elements like below, and want to get the index numbers of "date2" under following sorting scheme. "jan" "1992" "feb" "1992" "mar" "1992" ............ "dec" "1992" "jan" "1993" "feb" "1993" "mar" "1993" ............ "dec" "1993" "jan" "1994" "feb" "1994" "mar" "1994" ............ "dec" "1994" Can anyone help me please? -- View this message in context: http://www.nabble.com/Sorting-tp25401249p25401249.html Sent from the R help mailing list archive at Nabble.com.
Try this: data2[order(data2[,2], match(data2[,1], tolower(month.abb))),] On Fri, Sep 11, 2009 at 10:46 AM, megh <megh700004@yahoo.com> wrote:> > I have following object : > > > date2 > [,1] [,2] > [1,] "apr" "1992" > [2,] "aug" "1992" > [3,] "dec" "1992" > [4,] "feb" "1992" > [5,] "jan" "1992" > [6,] "jul" "1992" > [7,] "jun" "1992" > [8,] "mar" "1992" > [9,] "may" "1992" > [10,] "nov" "1992" > [11,] "oct" "1992" > [12,] "sep" "1992" > [13,] "apr" "1993" > [14,] "aug" "1993" > [15,] "dec" "1993" > [16,] "feb" "1993" > [17,] "jan" "1993" > [18,] "jul" "1993" > [19,] "jun" "1993" > [20,] "mar" "1993" > [21,] "may" "1993" > [22,] "nov" "1993" > [23,] "oct" "1993" > [24,] "sep" "1993" > [25,] "apr" "1994" > [26,] "aug" "1994" > [27,] "dec" "1994" > [28,] "feb" "1994" > [29,] "jan" "1994" > [30,] "jul" "1994" > > Now I want to sort the elements like below, and want to get the index > numbers of "date2" under following sorting scheme. > > "jan" "1992" > "feb" "1992" > "mar" "1992" > ............ > "dec" "1992" > "jan" "1993" > "feb" "1993" > "mar" "1993" > ............ > "dec" "1993" > "jan" "1994" > "feb" "1994" > "mar" "1994" > ............ > "dec" "1994" > > Can anyone help me please? > -- > View this message in context: > http://www.nabble.com/Sorting-tp25401249p25401249.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
You might want to add a date column it you are planning to operate on the data with dates:> a # your dataX..1. X..2. 1 apr 1992 2 aug 1992 3 dec 1992 4 feb 1992 5 jan 1992 6 jul 1992 7 jun 1992 8 mar 1992 9 may 1992 10 nov 1992 11 oct 1992 12 sep 1992 13 apr 1993 14 aug 1993 15 dec 1993 16 feb 1993 17 jan 1993 18 jul 1993 19 jun 1993 20 mar 1993 21 may 1993 22 nov 1993 23 oct 1993 24 sep 1993 25 apr 1994 26 aug 1994 27 dec 1994 28 feb 1994 29 jan 1994 30 jul 1994> # convert to date > a$date <- as.Date(paste('1', a[[1]], a[[2]]), "%d %b %Y") > aX..1. X..2. date 1 apr 1992 1992-04-01 2 aug 1992 1992-08-01 3 dec 1992 1992-12-01 4 feb 1992 1992-02-01 5 jan 1992 1992-01-01 6 jul 1992 1992-07-01 7 jun 1992 1992-06-01 8 mar 1992 1992-03-01 9 may 1992 1992-05-01 10 nov 1992 1992-11-01 11 oct 1992 1992-10-01 12 sep 1992 1992-09-01 13 apr 1993 1993-04-01 14 aug 1993 1993-08-01 15 dec 1993 1993-12-01 16 feb 1993 1993-02-01 17 jan 1993 1993-01-01 18 jul 1993 1993-07-01 19 jun 1993 1993-06-01 20 mar 1993 1993-03-01 21 may 1993 1993-05-01 22 nov 1993 1993-11-01 23 oct 1993 1993-10-01 24 sep 1993 1993-09-01 25 apr 1994 1994-04-01 26 aug 1994 1994-08-01 27 dec 1994 1994-12-01 28 feb 1994 1994-02-01 29 jan 1994 1994-01-01 30 jul 1994 1994-07-01> a[order(a$date),]X..1. X..2. date 5 jan 1992 1992-01-01 4 feb 1992 1992-02-01 8 mar 1992 1992-03-01 1 apr 1992 1992-04-01 9 may 1992 1992-05-01 7 jun 1992 1992-06-01 6 jul 1992 1992-07-01 2 aug 1992 1992-08-01 12 sep 1992 1992-09-01 11 oct 1992 1992-10-01 10 nov 1992 1992-11-01 3 dec 1992 1992-12-01 17 jan 1993 1993-01-01 16 feb 1993 1993-02-01 20 mar 1993 1993-03-01 13 apr 1993 1993-04-01 21 may 1993 1993-05-01 19 jun 1993 1993-06-01 18 jul 1993 1993-07-01 14 aug 1993 1993-08-01 24 sep 1993 1993-09-01 23 oct 1993 1993-10-01 22 nov 1993 1993-11-01 15 dec 1993 1993-12-01 29 jan 1994 1994-01-01 28 feb 1994 1994-02-01 25 apr 1994 1994-04-01 30 jul 1994 1994-07-01 26 aug 1994 1994-08-01 27 dec 1994 1994-12-01>On Fri, Sep 11, 2009 at 9:46 AM, megh <megh700004 at yahoo.com> wrote:> > I have following object : > >> date2 > ? ? ? [,1] ?[,2] > ?[1,] "apr" "1992" > ?[2,] "aug" "1992" > ?[3,] "dec" "1992" > ?[4,] "feb" "1992" > ?[5,] "jan" "1992" > ?[6,] "jul" "1992" > ?[7,] "jun" "1992" > ?[8,] "mar" "1992" > ?[9,] "may" "1992" > ?[10,] "nov" "1992" > ?[11,] "oct" "1992" > ?[12,] "sep" "1992" > ?[13,] "apr" "1993" > ?[14,] "aug" "1993" > ?[15,] "dec" "1993" > ?[16,] "feb" "1993" > ?[17,] "jan" "1993" > ?[18,] "jul" "1993" > ?[19,] "jun" "1993" > ?[20,] "mar" "1993" > ?[21,] "may" "1993" > ?[22,] "nov" "1993" > ?[23,] "oct" "1993" > ?[24,] "sep" "1993" > ?[25,] "apr" "1994" > ?[26,] "aug" "1994" > ?[27,] "dec" "1994" > ?[28,] "feb" "1994" > ?[29,] "jan" "1994" > ?[30,] "jul" "1994" > > Now I want to sort the elements like below, and want to get the index > numbers of "date2" under following sorting scheme. > > "jan" "1992" > "feb" "1992" > "mar" "1992" > ............ > "dec" "1992" > "jan" "1993" > "feb" "1993" > "mar" "1993" > ............ > "dec" "1993" > "jan" "1994" > "feb" "1994" > "mar" "1994" > ............ > "dec" "1994" > > Can anyone help me please? > -- > View this message in context: http://www.nabble.com/Sorting-tp25401249p25401249.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
months<-c("jan","feb","mar","apr","may","jun", "jul","aug","sep","oct","nov","dec") sortorder<-order(as.numeric(date2[,2]),match(date2[,1],months)) date2[sortorder,] is probably what you want. megh wrote:> I have following object : > >> date2 > [,1] [,2] > [1,] "apr" "1992" > [2,] "aug" "1992" > [3,] "dec" "1992" > [4,] "feb" "1992" > [5,] "jan" "1992" > [6,] "jul" "1992" > [7,] "jun" "1992" > [8,] "mar" "1992" > [9,] "may" "1992" > [10,] "nov" "1992" > [11,] "oct" "1992" > [12,] "sep" "1992" > [13,] "apr" "1993" > [14,] "aug" "1993" > [15,] "dec" "1993" > [16,] "feb" "1993" > [17,] "jan" "1993" > [18,] "jul" "1993" > [19,] "jun" "1993" > [20,] "mar" "1993" > [21,] "may" "1993" > [22,] "nov" "1993" > [23,] "oct" "1993" > [24,] "sep" "1993" > [25,] "apr" "1994" > [26,] "aug" "1994" > [27,] "dec" "1994" > [28,] "feb" "1994" > [29,] "jan" "1994" > [30,] "jul" "1994" > > Now I want to sort the elements like below, and want to get the index > numbers of "date2" under following sorting scheme. > > "jan" "1992" > "feb" "1992" > "mar" "1992" > ............ > "dec" "1992" > "jan" "1993" > "feb" "1993" > "mar" "1993" > ............ > "dec" "1993" > "jan" "1994" > "feb" "1994" > "mar" "1994" > ............ > "dec" "1994" > > Can anyone help me please?-- Erich Neuwirth, University of Vienna Faculty of Computer Science Computer Supported Didactics Working Group Visit our SunSITE at http://sunsite.univie.ac.at Phone: +43-1-4277-39464 Fax: +43-1-4277-39459
Hello, Say I have a dataset as followed: Category Value b 1 b 2 a 7 a 1 Then, if I: levels(Category) It will return: [a], [b] But I want to keep the original order, i.e.: [b], [a] Is it possible to do it in R? Thanks in advance! Chris -- View this message in context: http://www.nabble.com/Sorting-tp25531007p25531007.html Sent from the R help mailing list archive at Nabble.com.