Reputation: 853
I am having a question about sequencing dates in data.table by group.
I have this code that use to work in the past but now it is not working.
I have figured out that this issue is do to having a newer version of the data.table package.
The code just worked on an older version (1.8.x) and now it updated to 1.9.2 and the code isn't working.
Can anyone help me figure this out.
Here is some data:
test_data <- data.frame(group=c(1,2,3),
date=c("2011-01-01","2012-02-02","2013-03-03"),
date2=Sys.Date(),
stringsAsFactors=FALSE)
test_data[,"date"] <- as.Date(test_data[,"date"])
test_data[,"date2"] <- as.Date(test_data[,"date2"])
Here is my code"
library("data.table")
identifier <- "group"
results <- data.table(test_data)[,{s=seq(from=date,to=Sys.Date(),by="days")},
by=list(group,date)]
I am getting the following error:
Error in Ops.Date(del, by) : / not defined for "Date" objects
Thanks ahead of time!
Upvotes: 3
Views: 5919
Reputation: 21
you can try as.POSIXct
intstead of as.Date
. In my experience, data.table
works well with POSIXct
type rather than date
type
Upvotes: 2
Reputation: 49820
Looks like the date
column is being coerced to numeric as you can see with
data.table(test_data)[, date, by=list(group,date)]
# group date date
#1: 1 2011-01-01 14975
#2: 2 2012-02-02 15372
#3: 3 2013-03-03 15767
I think it's a regression since it worked with older versions of data.table, but you can work around by coercing back to Date.
data.table(test_data)[, seq(as.Date(date, origin="1970-01-01"), Sys.Date(), by="days"),
by=list(group,date)]
Upvotes: 0