Pam
Pam

Reputation: 111

Error in generating sequence of dates-'from' must be a finite number

I have a table and I am trying to generate a sequence of dates. It is a simple code and I tried several options and I cannot understand why I am getting an error.

df <- fread("
indexdt
01-02-2019
08-10-2019")

setDT(df)[,id := .I] 
df[, .(indexdt, dates = format(seq(as.Date(indexdt, "%m/%d/%Y"), 
                                    length.out=3, by = "1 day"), "%m/%d/%Y")) , by=id]

The error - Error in seq.int(from, by = by, length.out = length.out) : 'from' must be a finite number

Upvotes: 0

Views: 773

Answers (1)

akrun
akrun

Reputation: 887691

The issue is with the format,

%m-%d-%Y

instead of

%m/%d/%Y

If we make the correction, it works

df[, .(indexdt, dates = format(seq(as.Date(indexdt, "%m-%d-%Y"), 
                          length.out=3, by = "1 day"), "%m-%d-%Y")) , by=id]
#   id    indexdt      dates
#1:  1 01-02-2019 01-02-2019
#2:  1 01-02-2019 01-03-2019
#3:  1 01-02-2019 01-04-2019
#4:  2 08-10-2019 08-10-2019
#5:  2 08-10-2019 08-11-2019
#6:  2 08-10-2019 08-12-2019

Using the wrong format, results in NA values

df[, as.Date(indexdt, "%m/%d/%Y")]
#[1] NA NA

Upvotes: 1

Related Questions