-1

I'm pretty new to r but picking it up gradually. My question is, I want to make my date vector start from the earliest date rather from the newest. I have about 50 odd rows and want it in order of earliest first.

head(dates1) [1] "2016-03-04" "2016-02-26" "2016-02-19" "2016-02-12" "2016-02-05" "2016-01-29"

I've tried order() but it gives back numeric values, I want to keep them as dates.

Thanks if you can help.

Jozi
  • 21
  • 1
  • 2
  • 2
    Use `sort`. For details, type `?sort`. Fyi regarding `order`: http://stackoverflow.com/q/2315601/1191259 – Frank Mar 04 '16 at 16:38

1 Answers1

2

Try the following:

dates1 <- c("2016-03-04", "2016-02-26", 
            "2016-02-19", "2016-02-12", 
            "2016-02-05", "2016-01-29")
dates1 <- as.Date(dates1)
sort(dates1)

Order returns the indices, to get the same result you can do the following:

dates1[order(dates1)]
Raad
  • 2,675
  • 1
  • 13
  • 26