mlr-org / mlr3misc

Miscellaneous helper functions for mlr3
https://mlr3misc.mlr-org.com
GNU Lesser General Public License v3.0
11 stars 2 forks source link

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

Closed be-marc closed 4 years ago

be-marc commented 4 years ago
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)][]
}
mllg commented 4 years ago

Thanks, should be fixed now.