zhangnan
zhangnan

Reputation: 11

How can I generate date like this in R

I can use next code to generate a timeseries using R

seq(from = as.Date("2020-01-30"),
    to = as.Date("2020-03-01"),
    by = "1 days"))

like this

[1] "2020-01-30" "2020-01-31" "2020-02-01" "2020-02-02" "2020-02-03"
[6] "2020-02-04" "2020-02-05" "2020-02-06" "2020-02-07" "2020-02-08"
[11] "2020-02-09" "2020-02-10" "2020-02-11" "2020-02-12" "2020-02-13"
[16] "2020-02-14" "2020-02-15" "2020-02-16" "2020-02-17" "2020-02-18"
[21] "2020-02-19" "2020-02-20" "2020-02-21" "2020-02-22" "2020-02-23"
[26] "2020-02-24" "2020-02-25" "2020-02-26" "2020-02-27" "2020-02-28"
[31] "2020-02-29" "2020-03-01"

but I want to generate a date in descend order, How can I do?

Upvotes: 0

Views: 56

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389175

You can reverse your original sequence

rev(seq(from = as.Date("2020-01-30"),
        to = as.Date("2020-03-01"),
        by = "1 days"))

Or change dates in from and to.

seq(from = as.Date("2020-03-01"),
     to = as.Date("2020-01-30"),
     by = "-1 days")

Upvotes: 1

Related Questions