Dear 'HTH' R friends I have a small dataframe as given below. I need to sort this database based on date in a decending order. I am not sure whether I have defined the date column in a proper format. mydat<-data.frame(date = (c("1/31/2010", "2/28/2010", "3/31/2010", "4/30/2010", "5/31/2010", "6/30/2010", "7/31/2010", "8/31/2010", "9/30/2010", "10/31/2010", "11/30/2010", "12/28/2010")), total=c(429, 25, 239, 99, 100, 96, 18, 21, 10, 76, 101, 81), newspapers=c(103, 4, 37, 109, 52, 87, 17, 13, 10, 56, 87, 14))> mydatdate total newspapers 1 1/31/2010 429 103 2 2/28/2010 25 4 3 3/31/2010 239 37 4 4/30/2010 99 109 5 5/31/2010 100 52 6 6/30/2010 96 87 7 7/31/2010 18 17 8 8/31/2010 21 13 9 9/30/2010 10 10 10 10/31/2010 76 56 11 11/30/2010 101 87 12 12/28/2010 81 14 I need to sort this data in a DESCENDING order based on a date. I.e. I need to have date total newspapers 12/28/2010 81 14 11/30/2010 101 87 10/31/2010 76 56 ................................. .................................. 1/31/2010 429 103 When I tried mydat.sort <- mydat[order(mydat$date)]> mydat.sort <- mydat[order(mydat$date)]Error in `[.data.frame`(mydat, order(mydat$date)) : undefined columns selected Kindly guide Vincy Pyne [[alternative HTML version deleted]]
Henrique Dallazuanna
2010-Dec-30 11:31 UTC
[R] Sorting data.frame datewise in a descending order
Try this: mydat[order(as.Date(mydat$date, "%m/%d/%Y"), decreasing = TRUE),] On Thu, Dec 30, 2010 at 9:27 AM, Vincy Pyne <vincy_pyne@yahoo.ca> wrote:> Dear 'HTH' R friends > > I have a small dataframe as given below. I need to sort this database based > on date in a decending order. I am not sure whether I have defined the date > column in a proper format. > > mydat<-data.frame(date = (c("1/31/2010", "2/28/2010", "3/31/2010", > "4/30/2010", "5/31/2010", "6/30/2010", "7/31/2010", "8/31/2010", > "9/30/2010", "10/31/2010", "11/30/2010", "12/28/2010")), total=c(429, 25, > 239, 99, 100, 96, 18, 21, 10, 76, 101, 81), newspapers=c(103, 4, 37, 109, > 52, 87, 17, 13, 10, 56, 87, 14)) > > > > mydat > date total newspapers > 1 1/31/2010 429 103 > 2 2/28/2010 25 4 > 3 3/31/2010 239 37 > 4 4/30/2010 99 109 > 5 5/31/2010 100 52 > 6 6/30/2010 96 87 > 7 7/31/2010 18 17 > 8 8/31/2010 21 13 > 9 9/30/2010 10 10 > 10 10/31/2010 76 56 > 11 11/30/2010 101 87 > 12 12/28/2010 81 14 > > I need to sort this data in a DESCENDING order based on a date. I.e. I need > to have > > date total newspapers > 12/28/2010 81 14 > 11/30/2010 101 87 > 10/31/2010 76 56 > ................................. > .................................. > > 1/31/2010 429 103 > > When I tried > > mydat.sort <- mydat[order(mydat$date)] > > > mydat.sort <- mydat[order(mydat$date)] > > Error in `[.data.frame`(mydat, order(mydat$date)) : undefined columns > selected > > Kindly guide > > Vincy Pyne > > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > 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]]