Skip to content

as.data.table.matrix keeps row names for null data tables #3149

@mllg

Description

@mllg

Consider this example:

M = matrix(1:3, nrow = 3)
M = M[, integer(0)]

DT = as.data.table(M) # null data table

The resulting object DT is a "Null data.table" with dimensions c(0,0). This deviates from as.data.frame, but this is documented.

> nrow(DT)
[1] 0
> dim(DT)
[1] 0 0

Confusingly, the null data.table still has rownames:

rownames(DT)
[1] "1" "2" "3"

This is problematic, because the dimensions of a data.frame is calculated using the rownames:

> base::dim.data.frame(DT)
[1] 3 0

Of course there is a S3 method dim.data.table which returns c(0, 0). However, you get the wrong dimensions in C/C++ code.

I wonder if there is any advantage in keeping the row names. I first thought they are stored for the conversion of data.tables to data.frames (as.data.frame / setDF), but the row names seem to be ignored here, too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions