There are several things wrong with your code.
First, the following two instructions are equivalent, since the argument format
is "%Y-%m-%d"
default:
as.Date(base$DATE_END, "%Y-%m-%d")
as.Date(base$DATE_END)
Second, it’s better to turn the whole column DATE_END
in class Date
a single time, right at the beginning of the code, and then use it already as a date. This base is just an example.
base <- data.frame(DATE_END = c("2013-02-25", "2013-05-14", "2013-09-16"))
base$DATE_END <- as.Date(base$DATE_END)
Third, if the object is class Date
the method seq.Date
is automatically called when called seq(uma_data, etc)
. It’s not necessary, but it’s also not wrong, call for seq.Date
explicitly.
In addition, arguments in seq.Date
. The R needs to know the beginning of the sequence and information about the sequence, such as:
- The end and increment, arguments
to
and by
;
- The total length, argument
length.out
;
- The total length equal to the length of another variable, argument
along.with
.
Finally the code. Since you say you need to create another column with random dates and later than the column of dates you have, I start by knowing what the last date is and then create a sequence of dates from there to today. And is to choose randomly with sample
. In this case I am sampling without replacement. See help("sample")
.
ult <- max(base$DATE_END)
set.seed(9447) # Torna os resultados reprodutíveis
sqDt <- seq(ult + 1, Sys.Date(), by = "day")
base$DATE_ALEA <- sample(sqDt, nrow(base))
base
# DATE_END DATE_ALEA
#1 2013-02-25 2015-11-27
#2 2013-05-14 2017-10-24
#3 2013-09-16 2014-11-01
thank you very much!
– stacker