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

Inconsistent replacement of list column element with NULL in table with 1 row #5558

Open
tdhock opened this issue Dec 8, 2022 · 1 comment · May be fixed by #6167
Open

Inconsistent replacement of list column element with NULL in table with 1 row #5558

tdhock opened this issue Dec 8, 2022 · 1 comment · May be fixed by #6167

Comments

@tdhock
Copy link
Member

tdhock commented Dec 8, 2022

In base R data.frame we can replace an element of a list column with NULL via:

> DF1=data.frame(L=I(list("A")),i=1)
> DF1$L=list(NULL)
> DF1
     L i
1 NULL 1

However in data.table, doing that results in deleting the list column entirely:

> DT1=data.table(L=list("A"),i=1)
> DT1$L=list(NULL)
> DT1
       i
   <num>
1:     1

Request: can we make the above code do a replacement (like base R data.frame) instead of deleting the column?

The above issue is confusing because it is inconsistent with how data table handles replacement in tables with more than one row:

> DT2=data.table(L=list("B","C"),i=1)
> DT2$L <- list(NULL,NULL)
> DT2
        L     i
   <list> <num>
1:            1
2:            1
> DF2=data.frame(L=I(list("B","C")),i=1)
> DF2$L <- list(NULL,NULL)
> DF2
     L i
1 NULL 1
2 NULL 1

Actually, we can do the replacement (inconsistently) in data.table via

> DT1=data.table(L=list("A"),i=1)
> DT1$L=list(list(NULL))
> DT1
        L     i
   <list> <num>
1:            1
@avimallu
Copy link
Contributor

avimallu commented Dec 8, 2022

I can do this:

library(data.table)
DT1=data.table(L=list("A"),i=1)
DT1[, `:=`(L = list(NULL))]

that works, but oddly not

DT1[, L := list(NULL)]

which should be identical per data.table documentation.

@tdhock tdhock changed the title can not replace list column element with NULL in table with 1 row Inconsistent replacement of list column element with NULL in table with 1 row Dec 9, 2022
@joshhwuu joshhwuu linked a pull request Jun 4, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants