Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is.null does not work on null data.table in R [possible bug]?

Tags:

r

data.table

I believe this might be a bug. If so, I will report. Looking for confirm:

> is.null(data.table(NULL))
  [1] FALSE

I see the following in FAQ:

2.5 Is a NULL data.table the same as DT[0]? No, despite the print method indicating otherwise. Strictly speaking it's not possible to have is.null(data.table(NULL)) return FALSE. This FAQ may be revisited in future.

If this is the intended behavior: how does one check if the object is null? In particular, rbindlist of a NULL object returns a NULL data.table so how to check?

> is.null(rbindlist(NULL))
  [1] FALSE
like image 509
Alex Avatar asked Jan 15 '23 00:01

Alex


1 Answers

I'm not sure why the FAQ is phrased the way it is. However, a null data table is just an empty list:

> data.table:::null.data.table
function () 
{
    ans = list()
    setattr(ans, "class", c("data.table", "data.frame"))
    setattr(ans, "row.names", .set_row_names(0L))
    settruelength(ans, 0L)
    alloc.col(ans)
}

which isn't NULL. From ?list:

An empty pairlist, pairlist() is the same as NULL. This is different from list().

You could check for it by seeing whether it is of length 0 (length(DT) == 0). This is what data.table:::print.data.table does to decide whether to print "NULL data.table" or "Empty data table (0 rows) ...".

like image 168
user1935457 Avatar answered Jan 19 '23 11:01

user1935457