Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implicit re-casting of logical to Date fails #5344

Closed
MPagel opened this issue Mar 2, 2022 · 2 comments
Closed

implicit re-casting of logical to Date fails #5344

MPagel opened this issue Mar 2, 2022 · 2 comments

Comments

@MPagel
Copy link

MPagel commented Mar 2, 2022

using data.table 1.14.0 built under R version 3.6.3 in RStudio Version 1.1.463 session using R version 3.6.0

slightly modified and greatly expanded from https://stackoverflow.com/a/54081089/4228193

using
alldata <- data.table(1)[,:=(c("va", "vb", "vc", "vd"),NA)][,V1:=NULL][.0]
or
alldata <- data.table(1)[,:=(c("va", "vb", "vc", "vd"),NA)][,V1:=NULL][!is.na(va)]

creates an empty data.table with 4 named columns, which I intend to iteratively append to with the results of an fread (stored in filedata). You can dummy up a value for filedata with filedata <- data.table(1)[,:=(va="bob", vb="barbara ann", vc=4, vd=as.Date("1965-09-23"), V1=NULL)]

alldata[,lapply(.SD,class)] # 0-row data seeded with NA in each column

    va      vb      vc       vd

1: logical logical logical logical

filedata[,lapply(.SD,class)] # lines of real data that you are trying to merge

      va        vb      vc   vd

1: character character integer Date

rbindlist(list(alldata,filedata))

Error in rbindlist(list(alldata, filedata), use.names = FALSE) :
Class attribute on column 4 of item 2 does not match with column 4 of item 1.

Basically, a logical column can't be auto-recast to a Date data type. Nor is IDate class data allowed to be appended to the vd column of filedata (code and results not shown here).

the Date class itself appears to accept NA as a value, but this NA appears to be slightly different from a logical NA
filedata <- data.table(1)[,:=(va="bob", vb="barbara ann", vc=4, vd=as.Date(NA))][,V1:=NULL]
filedata

va vb vc vd
1: bob barbara ann 4 <NA>

filedata[,lapply(.SD, class)]

      va        vb      vc   vd

1: character character numeric Date

rbindlist(list(alldata,filedata))

Error in rbindlist(list(alldata, filedata)) :
Class attribute on column 4 of item 2 does not match with column 4 of item 1.

filedata <- data.table(1)[,:=(va="bob", vb="barbara ann", vc=4, vd=as.logical(NA))][,V1:=NULL]
filedata[,lapply(.SD, class)]

      va        vb      vc      vd

1: character character numeric logical

rbindlist(list(alldata,filedata)) # note the lack of <> brackets around NA

va          vb vc vd

1: bob barbara ann 4 NA

...but it seems you can convert from a logical to a Date with explicit casting outside of data.table...
as.integer(as.logical(NA))

[1] NA

as.character(as.logical(NA))

[1] NA

as.Date(as.logical(NA))

[1] NA

class(as.Date(as.logical(NA)))

[1] "Date"

@avimallu
Copy link
Contributor

avimallu commented Mar 4, 2022

Related #5309, and probably original known from #3911.

ReprEx:

library(data.table)
DT1 = data.table(NA)
DT2 = data.table(as.Date(NA))
rbindlist(list(DT1, DT2))

@MPagel MPagel closed this as completed Apr 8, 2022
@MPagel
Copy link
Author

MPagel commented Apr 8, 2022

Please mark as a duplicate or supplementary to #3911

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants