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

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

Closed
be-marc opened this issue Apr 4, 2020 · 1 comment
Closed

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

be-marc opened this issue Apr 4, 2020 · 1 comment

Comments

@be-marc
Copy link
Sponsor Member

be-marc commented Apr 4, 2020

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
Copy link
Sponsor Member

mllg commented Apr 11, 2020

Thanks, should be fixed now.

@mllg mllg closed this as completed Apr 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants