Hello, How is it possible to sort dates in R? Cheers, Carol
? sort x <- c(Sys.Date(), Sys.Date() + 1, Sys.Date() - 1) print(x) print(sort(x)) Michael On Fri, Mar 9, 2012 at 8:35 AM, carol white <wht_crl at yahoo.com> wrote:> Hello, > How is it possible to sort dates in R? > > Cheers, > > Carol > > ______________________________________________ > 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.
Hi> > Hello, > How is it possible to sort dates in R?You mean sort? Or order? Something like:> dd <-sample(Sys.Date()-1:10) > dd[1] "2012-03-04" "2012-03-05" "2012-02-29" "2012-03-01" "2012-03-02" [6] "2012-03-08" "2012-03-03" "2012-02-28" "2012-03-06" "2012-03-07"> sort(dd)[1] "2012-02-28" "2012-02-29" "2012-03-01" "2012-03-02" "2012-03-03" [6] "2012-03-04" "2012-03-05" "2012-03-06" "2012-03-07" "2012-03-08"> dd[order(dd)][1] "2012-02-28" "2012-02-29" "2012-03-01" "2012-03-02" "2012-03-03" [6] "2012-03-04" "2012-03-05" "2012-03-06" "2012-03-07" "2012-03-08">Regards Petr> > Cheers, > > Carol > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
>How is it possible to sort dates in R?Try this:>a = sample(as.Date(1:100, origin = '2012-01-01'),15) >a[1] "2012-01-31" "2012-01-22" "2012-03-18" "2012-03-05" "2012-03-17" [6] "2012-03-08" "2012-01-08" "2012-01-20" "2012-03-01" "2012-03-21" [11] "2012-02-17" "2012-01-17" "2012-02-12" "2012-02-28" "2012-04-01">sort(a)[1] "2012-01-08" "2012-01-17" "2012-01-20" "2012-01-22" "2012-01-31" [6] "2012-02-12" "2012-02-17" "2012-02-28" "2012-03-01" "2012-03-05" [11] "2012-03-08" "2012-03-17" "2012-03-18" "2012-03-21" "2012-04-01">Cheers, >Carol______________________________________________ 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. ------------------------------------------ Robert W. Baer, Ph.D. Professor of Physiology Kirksville College of Osteopathic Medicine A. T. Still University of Health Sciences 800 W. Jefferson St. Kirksville, MO 63501 660-626-2322 FAX 660-626-2965
On Fri, Mar 9, 2012 at 8:35 AM, carol white <wht_crl at yahoo.com> wrote:> Hello, > How is it possible to sort dates in R? >Your question has already been answered but note that if your data is a time series and you represent it using zoo it will automatically be sorted. Here dates is in reverse chronological order whereas z, the zoo time series object, is in chronological order: values <- 13:10 dates <- as.Date("2000-01-01") + 3:0; dates library(zoo) z <- zoo(values, dates); z -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com