Generate a date after a specific date

Asked

Viewed 95 times

3

I have a column of a table with dates, I need to create another column with random dates and after this one. I used 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 column DATE_END have a face 2013-09-16, for example. There is no blank date cell, all are in the same format.

2 answers

6


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!

3

Whoa, that’s all right?

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

# 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.