Generate a date after a specific date

3

I have a column of a table with dates, I need to create another column with random dates and after that date. I used the seq.Date function for this but I get the following error.

date_arq<-seq.Date(as.Date(base$DATE_END, "%Y-%m-%d"))
Error in seq.Date(as.Date(base$DATE_END, "%Y-%m-%d")) : 
  'from' deve ter comprimento 1

The dates in the DATE_END column have a 2013-09-16 face, for example. There is no cell with a blank date, all are in the same format.

    
asked by anonymous 06.06.2018 / 20:00

2 answers

6

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
    
06.06.2018 / 20:38
3

Hello, how are you?

You can also do this without using the seq() function:

# Criando um data frame de exemplo
base <- data.frame(
  DATE_END = as.Date(c("2017-02-15", "2017-04-08", "2017-09-13", "2017-11-20"))
)

# Identificando a maior data na coluna DATA_END
start <- max(base$DATE_END)

# Definindo faixa de datas aleatórias (data final - data inicial)
# Como exemplo adotei o Sys.Date() como data final
range <- Sys.Date() - start+1

# Garantindo a reprodução dos resultados aleatórios
set.seed(101)

# Criando a coluna DATA_SEQ e atribuindo as datas aleatórias
# Note que o a função nrow(base) vai garantir que a amostragem não ultrapasse 
# a quantidade de linhas do data frame
base$DATE_SEQ <- sample(start+1:range,nrow(base))

# Se quiser deixar as datas aleatórias em ordem crescente
base$DATE_SEQ <- sort(sample(start+1:range,nrow(base)))

print(base)

# DATE_END   DATE_SEQ
# 1 2017-02-15 2017-11-29
# 2 2017-04-08 2018-02-03
# 3 2017-09-13 2018-03-29
# 4 2017-11-20 2018-04-09
    
07.06.2018 / 03:22