create date vector, change to factor and format

I have the following code:

gsub("-","/",paste(cut(seq(as.POSIXct(Sys.Date(),format="%d-%b-%y"), by = "-1 day", length.out = 10),"days"),collapse = ","))

The output:

"2019/03/20,2019/03/19,2019/03/18,2019/03/17,2019/03/16,2019/03/15,2019/03/14,2019/03/13,2019/03/12,2019/03/11"

However the desired result is

'20/03/2019','19/03/2019','18/03/2019','17/03/2019','16/03/2019','15/03/2019','14/03/2019','13/03/2019','12/03/2019','11/03/2019'

How can I accomplish that ?

Regards

Upvotes: 0

Views: 27

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389275

Not sure what you are trying to do but you can generate the required output by doing

format(Sys.Date() - 1:10, "%d/%m/%Y")

#[1] "20/03/2019" "19/03/2019" "18/03/2019" "17/03/2019" "16/03/2019" "15/03/2019" 
#    "14/03/2019" "13/03/2019" "12/03/2019" "11/03/2019"

Upvotes: 4

Related Questions