[datatable-help] Replace missings by dates in data table

Gabor Grothendieck ggrothendieck at gmail.com
Tue Sep 16 19:27:13 CEST 2014


On Tue, Sep 16, 2014 at 1:04 PM, Frank S. <f_j_rod at hotmail.com> wrote:
> Hi to all members of the list,
>
>
>
> Let's say I have the following data (small example):
>
>
>
> DT <- data.table(ID=c(1,1,2),
>         start=c("1985-01-01","1993-07-15","1993-05-17"),
>         end=c("1992-05-01","1997-02-01",NA))
>
> I would want to replace missing values by "01-01-2000" in "end" variable,
> and convert both "start" and "end" columns in as.Date class.
>
> I tried the code:
>
>
>
> DT[ , c("start",
> "end"):=list(as.Date(start,format="%d/%m/%Y",origin="1900-10-01"),
>
> as.Date(ifelse(is.na(end),"01/01/2000",end),format="%d/%m/%Y",origin="1900-10-01")),
> by=ID]
>
>
>
> Error in `[.data.table`(DT, , `:=`(c("start", "end"), list(as.Date(start,  :
>   Type of RHS ('double') must match LHS ('character').
>
>
>
> What I have to change? Am I doing it in a too complicated way?

DT[, list(ID, start = as.Date(start), end = as.Date(replace(end,
is.na(end), "2000-01-01")))]


More information about the datatable-help mailing list