Katherine Gobin
2013-Apr-15 06:01 UTC
[R] Sorting data.frame and again sorting within data.frame
Dear R forum, I have a data.frame as defied below - df = data.frame(names = c("C", "A", "A", "B", "C", "B", "A", "B", "C"), dates = c("4/15/2013", "4/13/2013", "4/15/2013", "4/13/2013", "4/13/2013", "4/15/2013", "4/14/2013", "4/14/2013","4/14/2013" ),values = c(10, 31, 31, 17, 11, 34, 102, 47, 29))> dfnames dates values 1 C 4/15/2013 10 2 A 4/13/2013 31 3 A 4/15/2013 31 4 B 4/13/2013 17 5 C 4/13/2013 11 6 B 4/15/2013 34 7 A 4/14/2013 102 8 B 4/14/2013 47 9 C 4/14/2013 29 I need to sort df first on "names" in increasing order and then further on "dates" in a decreasing order i.e. I need names dates values A 4/15/2013 31 A 4/14/2013 102 A 4/13/2013 31 B 4/15/2013 34 B 4/14/2013 47 B 4/13/2013 17 C 4/15/2013 10 C 4/14/2013 29 C 4/13/2013 11 I tried df_sorted = df[order(df$names, (as.Date(df$dates, "%m/%d/%Y")), decreasing = TRUE),]> df_sortednames dates values 1 C 4/15/2013 10 9 C 4/14/2013 29 5 C 4/13/2013 11 6 B 4/15/2013 34 8 B 4/14/2013 47 4 B 4/13/2013 17 3 A 4/15/2013 31 7 A 4/14/2013 102 2 A 4/13/2013 31 I need A to appear first with all three corresponding dates in decreasing order, then B and so on. Please guide. With regards Katherine [[alternative HTML version deleted]]
Jeff Newmiller
2013-Apr-15 07:40 UTC
[R] Sorting data.frame and again sorting within data.frame
The examples in ?order show a method that could be applied if you avoid the decreasing argument and instead convert the Date to numeric for purposes of sorting. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Katherine Gobin <katherine_gobin at yahoo.com> wrote:>Dear R forum, > >I have a data.frame as defied below - > >df = data.frame(names = c("C", "A", "A", "B", "C", "B", "A", "B", "C"), >dates = c("4/15/2013", "4/13/2013", "4/15/2013", "4/13/2013", >"4/13/2013", "4/15/2013", "4/14/2013", "4/14/2013","4/14/2013" ),values >= c(10, 31, 31, 17, 11, 34, 102, 47, 29)) > >> df >? names???? dates values >1???? C 4/15/2013???? 10 >2???? A 4/13/2013???? 31 >3???? A 4/15/2013???? 31 >4???? B 4/13/2013???? 17 >5???? C 4/13/2013???? 11 >6???? B > 4/15/2013???? 34 >7???? A 4/14/2013??? 102 >8???? B 4/14/2013???? 47 >9???? C 4/14/2013???? 29 > >I need to sort df first on "names" in increasing order and then further >on "dates" in a decreasing order i.e. I need > >names??????? dates??????? values >A??????????? 4/15/2013????? 31 >A??????????? 4/14/2013???? 102 >A??????????? 4/13/2013????? 31 >B??????????? 4/15/2013????? > 34 >B??????????? 4/14/2013????? 47 >B??????????? 4/13/2013????? 17 >C??????????? 4/15/2013????? 10 >C??????????? 4/14/2013????? 29 >C??????????? 4/13/2013????? 11 > >I tried > >df_sorted = df[order(df$names, (as.Date(df$dates, "%m/%d/%Y")), >decreasing = TRUE),] > >> df_sorted >? names???? dates values >1???? C 4/15/2013???? 10 >9???? C 4/14/2013???? 29 >5???? C 4/13/2013???? > 11 >6???? B 4/15/2013???? 34 >8???? B 4/14/2013???? 47 >4???? B 4/13/2013???? 17 >3???? A 4/15/2013???? 31 >7???? A 4/14/2013??? 102 >2???? A 4/13/2013???? 31 > > >I need A to appear first with all three corresponding dates in >decreasing order, then B and so on. > >Please guide. > >With regards > >Katherine > > > [[alternative HTML version deleted]] > > > >------------------------------------------------------------------------ > >______________________________________________ >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.
library(plyr) arrange(df,names,desc(dates)) #? names???? dates values #1???? A 4/15/2013???? 31 #2???? A 4/14/2013??? 102 #3???? A 4/13/2013???? 31 #4???? B 4/15/2013???? 34 #5???? B 4/14/2013???? 47 #6???? B 4/13/2013???? 17 #7???? C 4/15/2013???? 10 #8???? C 4/14/2013???? 29 #9???? C 4/13/2013???? 11 #or df[with(df,order(names,desc(dates))),] #? names???? dates values #3???? A 4/15/2013???? 31 #7???? A 4/14/2013??? 102 #2???? A 4/13/2013???? 31 #6???? B 4/15/2013???? 34 #8???? B 4/14/2013???? 47 #4???? B 4/13/2013???? 17 #1???? C 4/15/2013???? 10 #9???? C 4/14/2013???? 29 #5???? C 4/13/2013???? 11 A.K. ----- Original Message ----- From: Katherine Gobin <katherine_gobin at yahoo.com> To: r-help at r-project.org Cc: Sent: Monday, April 15, 2013 2:01 AM Subject: [R] Sorting data.frame and again sorting within data.frame Dear R forum, I have a data.frame as defied below - df = data.frame(names = c("C", "A", "A", "B", "C", "B", "A", "B", "C"), dates = c("4/15/2013", "4/13/2013", "4/15/2013", "4/13/2013", "4/13/2013", "4/15/2013", "4/14/2013", "4/14/2013","4/14/2013" ),values = c(10, 31, 31, 17, 11, 34, 102, 47, 29))> df? names???? dates values 1???? C 4/15/2013???? 10 2???? A 4/13/2013???? 31 3???? A 4/15/2013???? 31 4???? B 4/13/2013???? 17 5???? C 4/13/2013???? 11 6???? B 4/15/2013???? 34 7???? A 4/14/2013??? 102 8???? B 4/14/2013???? 47 9???? C 4/14/2013???? 29 I need to sort df first on "names" in increasing order and then further on "dates" in a decreasing order i.e. I need names??????? dates??????? values A??????????? 4/15/2013????? 31 A??????????? 4/14/2013???? 102 A??????????? 4/13/2013????? 31 B??????????? 4/15/2013????? 34 B??????????? 4/14/2013????? 47 B??????????? 4/13/2013????? 17 C??????????? 4/15/2013????? 10 C??????????? 4/14/2013????? 29 C??????????? 4/13/2013????? 11 I tried df_sorted = df[order(df$names, (as.Date(df$dates, "%m/%d/%Y")), decreasing = TRUE),]> df_sorted? names???? dates values 1???? C 4/15/2013???? 10 9???? C 4/14/2013???? 29 5???? C 4/13/2013???? 11 6???? B 4/15/2013???? 34 8???? B 4/14/2013???? 47 4???? B 4/13/2013???? 17 3???? A 4/15/2013???? 31 7???? A 4/14/2013??? 102 2???? A 4/13/2013???? 31 I need A to appear first with all three corresponding dates in decreasing order, then B and so on. Please guide. With regards Katherine ??? [[alternative HTML version deleted]] ______________________________________________ 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.
You can also use: df[order(df$names,-xtfrm(df$dates),decreasing=FALSE),] ?# names???? dates values #3???? A 4/15/2013???? 31 #7???? A 4/14/2013??? 102 #2???? A 4/13/2013???? 31 #6???? B 4/15/2013???? 34 #8???? B 4/14/2013???? 47 #4???? B 4/13/2013???? 17 #1???? C 4/15/2013???? 10 #9???? C 4/14/2013???? 29 #5???? C 4/13/2013???? 11 A.K. ----- Original Message ----- From: arun <smartpink111 at yahoo.com> To: Katherine Gobin <katherine_gobin at yahoo.com> Cc: R help <r-help at r-project.org> Sent: Monday, April 15, 2013 8:57 AM Subject: Re: [R] Sorting data.frame and again sorting within data.frame library(plyr) arrange(df,names,desc(dates)) #? names???? dates values #1???? A 4/15/2013???? 31 #2???? A 4/14/2013??? 102 #3???? A 4/13/2013???? 31 #4???? B 4/15/2013???? 34 #5???? B 4/14/2013???? 47 #6???? B 4/13/2013???? 17 #7???? C 4/15/2013???? 10 #8???? C 4/14/2013???? 29 #9???? C 4/13/2013???? 11 #or df[with(df,order(names,desc(dates))),] #? names???? dates values #3???? A 4/15/2013???? 31 #7???? A 4/14/2013??? 102 #2???? A 4/13/2013???? 31 #6???? B 4/15/2013???? 34 #8???? B 4/14/2013???? 47 #4???? B 4/13/2013???? 17 #1???? C 4/15/2013???? 10 #9???? C 4/14/2013???? 29 #5???? C 4/13/2013???? 11 A.K. ----- Original Message ----- From: Katherine Gobin <katherine_gobin at yahoo.com> To: r-help at r-project.org Cc: Sent: Monday, April 15, 2013 2:01 AM Subject: [R] Sorting data.frame and again sorting within data.frame Dear R forum, I have a data.frame as defied below - df = data.frame(names = c("C", "A", "A", "B", "C", "B", "A", "B", "C"), dates = c("4/15/2013", "4/13/2013", "4/15/2013", "4/13/2013", "4/13/2013", "4/15/2013", "4/14/2013", "4/14/2013","4/14/2013" ),values = c(10, 31, 31, 17, 11, 34, 102, 47, 29))> df? names???? dates values 1???? C 4/15/2013???? 10 2???? A 4/13/2013???? 31 3???? A 4/15/2013???? 31 4???? B 4/13/2013???? 17 5???? C 4/13/2013???? 11 6???? B 4/15/2013???? 34 7???? A 4/14/2013??? 102 8???? B 4/14/2013???? 47 9???? C 4/14/2013???? 29 I need to sort df first on "names" in increasing order and then further on "dates" in a decreasing order i.e. I need names??????? dates??????? values A??????????? 4/15/2013????? 31 A??????????? 4/14/2013???? 102 A??????????? 4/13/2013????? 31 B??????????? 4/15/2013????? 34 B??????????? 4/14/2013????? 47 B??????????? 4/13/2013????? 17 C??????????? 4/15/2013????? 10 C??????????? 4/14/2013????? 29 C??????????? 4/13/2013????? 11 I tried df_sorted = df[order(df$names, (as.Date(df$dates, "%m/%d/%Y")), decreasing = TRUE),]> df_sorted? names???? dates values 1???? C 4/15/2013???? 10 9???? C 4/14/2013???? 29 5???? C 4/13/2013???? 11 6???? B 4/15/2013???? 34 8???? B 4/14/2013???? 47 4???? B 4/13/2013???? 17 3???? A 4/15/2013???? 31 7???? A 4/14/2013??? 102 2???? A 4/13/2013???? 31 I need A to appear first with all three corresponding dates in decreasing order, then B and so on. Please guide. With regards Katherine ??? [[alternative HTML version deleted]] ______________________________________________ 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.
David Winsemius
2013-Apr-15 15:58 UTC
[R] Sorting data.frame and again sorting within data.frame
On Apr 14, 2013, at 11:01 PM, Katherine Gobin wrote:> Dear R forum, > > I have a data.frame as defied below - > > df = data.frame(names = c("C", "A", "A", "B", "C", "B", "A", "B", "C"), dates = c("4/15/2013", "4/13/2013", "4/15/2013", "4/13/2013", "4/13/2013", "4/15/2013", "4/14/2013", "4/14/2013","4/14/2013" ),values = c(10, 31, 31, 17, 11, 34, 102, 47, 29)) > >> df > names dates values > 1 C 4/15/2013 10 > 2 A 4/13/2013 31 > 3 A 4/15/2013 31 > 4 B 4/13/2013 17 > 5 C 4/13/2013 11 > 6 B > 4/15/2013 34 > 7 A 4/14/2013 102 > 8 B 4/14/2013 47 > 9 C 4/14/2013 29 > > I need to sort df first on "names" in increasing order and then further on "dates" in a decreasing order i.e. I need >So far no one has pointed out that these are not really "Dates" in the R sense and will not sort correctly if any of the proposed methods are applied to sequences that extend beyond6 months, i.e, until October forward. You would be advised to convert to real Date-classed variables. ?strptime ?as.Date -- David> names dates values > A 4/15/2013 31 > A 4/14/2013 102 > A 4/13/2013 31 > B 4/15/2013 > 34 > B 4/14/2013 47 > B 4/13/2013 17 > C 4/15/2013 10 > C 4/14/2013 29 > C 4/13/2013 11 > > I tried > > df_sorted = df[order(df$names, (as.Date(df$dates, "%m/%d/%Y")), decreasing = TRUE),] > >> df_sorted > names dates values > 1 C 4/15/2013 10 > 9 C 4/14/2013 29 > 5 C 4/13/2013 > 11 > 6 B 4/15/2013 34 > 8 B 4/14/2013 47 > 4 B 4/13/2013 17 > 3 A 4/15/2013 31 > 7 A 4/14/2013 102 > 2 A 4/13/2013 31 > > > I need A to appear first with all three corresponding dates in decreasing order, then B and so on. > > Please guide. > > With regards > > Katherine > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.David Winsemius Alameda, CA, USA
Jeff Newmiller
2013-Apr-15 16:33 UTC
[R] Sorting data.frame and again sorting within data.frame
Yes, that would be because she converted to Date on the fly in her example, and so apparently did not need this reminder. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. David Winsemius <dwinsemius at comcast.net> wrote:> >On Apr 14, 2013, at 11:01 PM, Katherine Gobin wrote: > >> Dear R forum, >> >> I have a data.frame as defied below - >> >> df = data.frame(names = c("C", "A", "A", "B", "C", "B", "A", "B", >"C"), dates = c("4/15/2013", "4/13/2013", "4/15/2013", "4/13/2013", >"4/13/2013", "4/15/2013", "4/14/2013", "4/14/2013","4/14/2013" ),values >= c(10, 31, 31, 17, 11, 34, 102, 47, 29)) >> >>> df >> names dates values >> 1 C 4/15/2013 10 >> 2 A 4/13/2013 31 >> 3 A 4/15/2013 31 >> 4 B 4/13/2013 17 >> 5 C 4/13/2013 11 >> 6 B >> 4/15/2013 34 >> 7 A 4/14/2013 102 >> 8 B 4/14/2013 47 >> 9 C 4/14/2013 29 >> >> I need to sort df first on "names" in increasing order and then >further on "dates" in a decreasing order i.e. I need >> > >So far no one has pointed out that these are not really "Dates" in the >R sense and will not sort correctly if any of the proposed methods are >applied to sequences that extend beyond6 months, i.e, until October >forward. You would be advised to convert to real Date-classed >variables. > >?strptime >?as.Date
David Winsemius
2013-Apr-15 17:19 UTC
[R] Sorting data.frame and again sorting within data.frame
On Apr 15, 2013, at 9:33 AM, Jeff Newmiller wrote:> Yes, that would be because she converted to Date on the fly in her example, and so apparently did not need this reminder.I apologize, Iobviously missed that. So the answer was simply to put a minus sign in front of the as.Date() expression. And that was what you were hoping she would see when she looked at the second and third examples help page that even had helpful comments. As you suggested: df_sorted = df[order(df$names, -as.numeric( as.Date(df$dates, "%m/%d/%Y") ), ] -- David ---------------------------------------------------------------------------> > Jeff Newmiller The ..... ..... Go Live... > DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... > Live: OO#.. Dead: OO#.. Playing > Research Engineer (Solar/Batteries O.O#. #.O#. with > /Software/Embedded Controllers) .OO#. .OO#. rocks...1k > --------------------------------------------------------------------------- > Sent from my phone. Please excuse my brevity. > > David Winsemius <dwinsemius at comcast.net> wrote: > >> >> On Apr 14, 2013, at 11:01 PM, Katherine Gobin wrote: >> >>> Dear R forum, >>> >>> I have a data.frame as defied below - >>> >>> df = data.frame(names = c("C", "A", "A", "B", "C", "B", "A", "B", >> "C"), dates = c("4/15/2013", "4/13/2013", "4/15/2013", "4/13/2013", >> "4/13/2013", "4/15/2013", "4/14/2013", "4/14/2013","4/14/2013" ),values >> = c(10, 31, 31, 17, 11, 34, 102, 47, 29)) >>> >>>> df >>> names dates values >>> 1 C 4/15/2013 10 >>> 2 A 4/13/2013 31 >>> 3 A 4/15/2013 31 >>> 4 B 4/13/2013 17 >>> 5 C 4/13/2013 11 >>> 6 B >>> 4/15/2013 34 >>> 7 A 4/14/2013 102 >>> 8 B 4/14/2013 47 >>> 9 C 4/14/2013 29 >>> >>> I need to sort df first on "names" in increasing order and then >> further on "dates" in a decreasing order i.e. I need >>> >> >> So far no one has pointed out that these are not really "Dates" in the >> R sense and will not sort correctly if any of the proposed methods are >> applied to sequences that extend beyond6 months, i.e, until October >> forward. You would be advised to convert to real Date-classed >> variables. >> >> ?strptime >> ?as.Date >David Winsemius Alameda, CA, USA