Skip to content

unnest() fails if column name y is present #42

@be-marc

Description

@be-marc
a = data.table(y = list(0), opt_x = list(list(z_1 = 100, z_2 = 200)))
unnest(a, "opt_x")
# >    x y z_1 z_2
# > 1: 1 0   0   0

a = data.table(z = list(0), opt_x = list(list(z_1 = 100, z_2 = 200)))
unnest(a, "opt_x")
# >    x z z_1 z_2
# > 1: 1 0 100 200

The bug occurs when the unnested columns are added to the data.table with rcbind since y is used as the variable name for the unnested columns.

rcbind
function (x, y) 
{
    assert_data_table(x)
    assert_data_table(y)
    if (ncol(x) == 0L) {
        return(y)
    }
    if (ncol(y) == 0L) {
        return(x)
    }
    if (nrow(x) != nrow(y)) {
        stopf("Tables have different number of rows (x: %i, y: %i)", 
            nrow(x), nrow(y))
    }
    dup = intersect(names(x), names(y))
    if (length(dup)) {
        stopf("Duplicated names: %s", str_collapse(dup))
    }
    x[, `:=`(names(y), y)][]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions