normally I use this:
seq(as.Date("01/01/1964"), as.Date("01/12/1964"), "months", format = "%d%m%Y", tz = "UTC")
but I'm receiving this:
[1] "0001-01-19" "0001-02-19" "0001-03-19" "0001-04-19" "0001-05-19" "0001-06-19" "0001-07-19" "0001-08-19" "0001-09-19"
[10] "0001-10-19" "0001-11-19" "0001-12-19"
I try lubridate and others tips from others ans, but not works.
Thanks in advance.
EDIT
@r2evans ans:
format(seq(as.Date('1964/01/01'), as.Date('1964/12/01'), 'months'), format="%d-%m-%Y", tz="UTC")
[1] "01-01-1964" "01-02-1964" "01-03-1964" "01-04-1964" "01-05-1964" "01-06-1964" "01-07-1964" "01-08-1964" "01-09-1964"
[10] "01-10-1964" "01-11-1964" "01-12-1964"
From help(as.Date)
:
format: A character string. If not specified, it will try '"%Y-%m-%d"' then '"%Y/%m/%d"' on the first non-'NA' element.
For debugging, it would have been informative to try:
as.Date("01/01/1964")
## [1] "0001-01-19"
from which you could have quickly deduced that it was mis-identifying your fields.
If you have control over your start/end date format, then simply change it:
seq(as.Date("1964/01/01"), as.Date("1964/12/01"), "months", format = "%d%m%Y", tz = "UTC")
## [1] "1964-01-01" "1964-02-01" "1964-03-01" "1964-04-01" "1964-05-01" "1964-06-01"
## [7] "1964-07-01" "1964-08-01" "1964-09-01" "1964-10-01" "1964-11-01" "1964-12-01"
Though the format=
parameter isn't working (seq
doesn't use it). Wrap the whole thing with format
to get the strings you appear to want, though realize that these will no longer be of Date
class:
format(seq(as.Date('1964/01/01'), as.Date('1964/12/01'), 'months'), format="%Y%m%d", tz="UTC")
## [1] "19640101" "19640201" "19640301" "19640401" "19640501" "19640601" "19640701"
## [8] "19640801" "19640901" "19641001" "19641101" "19641201"
If, however, you do not have control over the format of the start/stop dates, then add a format=
parameter (and tz=
if necessary) to each as.Date
:
seq(as.Date('01/01/1964', format='%d/%m/%Y'), as.Date('01/12/1964', format='%d/%m/%Y'), 'months')
## [1] "1964-01-01" "1964-02-01" "1964-03-01" "1964-04-01" "1964-05-01" "1964-06-01"
## [7] "1964-07-01" "1964-08-01" "1964-09-01" "1964-10-01" "1964-11-01" "1964-12-01"
and use format
as above, if needed.