Skip to content

New [[.tbl_df shouldn't use vec_slice() when doing [[i, j]] #681

@DavisVaughan

Description

@DavisVaughan

CRAN tibble

library(tibble)

tbl <- tibble(x = structure(c(1, 2), extra = "hi"))

tbl$x
#> [1] 1 2
#> attr(,"extra")
#> [1] "hi"

tbl[[1, 1]]
#> [1] 1

Dev tibble

library(tibble)

tbl <- tibble(x = structure(c(1, 2), extra = "hi"))

tbl$x
#> [1] 1 2
#> attr(,"extra")
#> [1] "hi"

tbl[[1, 1]]
#> [1] 1
#> attr(,"extra")
#> [1] "hi"

I think this is definitely not right. [[ should always strip attributes from atomic vectors.

The issue comes from using vec_slice() in [[.tbl_df here:

vec_slice(x, i)

The issue is that vec_slice() works more like [ which won't strip the attributes from the column. [[ would. The vctrs equivalent for [[ doesn't exist yet, but would be vec_get()
r-lib/vctrs#626

Note that base:::[[.data.frame uses [[ for the row extraction after selecting the column, which is why we previously had the correct behavior:

> base:::`[[.data.frame`
function (x, ..., exact = TRUE) 
{
    na <- nargs() - !missing(exact)
    if (!all(names(sys.call()) %in% c("", "exact"))) 
        warning("named arguments other than 'exact' are discouraged")
    if (na < 3L) 
        (function(x, i, exact) if (is.matrix(i)) 
            as.matrix(x)[[i]]
        else .subset2(x, i, exact = exact))(x, ..., exact = exact)
    else {
        col <- .subset2(x, ..2, exact = exact)
        i <- if (is.character(..1)) 
            pmatch(..1, row.names(x), duplicates.ok = TRUE)
        else ..1
        col[[i, exact = exact]] # <- RIGHT HERE!
    }
}

vec_get() also behaves differently than vec_slice() with lists, so list column extraction is also different now.

CRAN tibble:

library(tibble)

tbl <- tibble(x = list(1, 2), y = 1:2)

tbl[[1, 1]]
#> [1] 1

Dev tibble:

library(tibble)

tbl <- tibble(x = list(1, 2), y = 1:2)

tbl[[1, 1]]
#> [[1]]
#> [1] 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions