There are several things wrong with your code.
First, the following two statements are equivalent, since the argument format
is "%Y-%m-%d"
by default:
as.Date(base$DATE_END, "%Y-%m-%d")
as.Date(base$DATE_END)
Secondly, it is best to turn the entire column DATE_END
into class Date
once, just at the beginning of the code, and then use it already as a date. This 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 of class Date
the method seq.Date
is automatically called when it is called seq(uma_data, etc)
. It is not necessary, but it is also not wrong , to call seq.Date
explicitly.
In addition, arguments are missing 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. As you say you need to create another column with random dates and after the column of dates you have, start by knowing the last date and then create a sequence of dates from then to today. And it is to randomly choose 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