tibble icon indicating copy to clipboard operation
tibble copied to clipboard

`[` can create non-unique names

Open hadley opened this issue 3 years ago • 4 comments

tibble::tibble(x = 1)[rep(1, 3)]
#> # A tibble: 1 × 3
#>       x     x     x
#>   <dbl> <dbl> <dbl>
#> 1     1     1     1

Created on 2022-09-16 with reprex v2.0.2

hadley avatar Sep 16 '22 20:09 hadley

This is documented in vignette("invariants"):

When subsetting repeated indexes, the resulting column names are undefined, do not rely on them.

Do we have reasons to change this behavior now?

krlmlr avatar Sep 24 '22 07:09 krlmlr

It just seems unusual that we don't apply our standard name repair rules here.

hadley avatar Sep 24 '22 13:09 hadley

I remember discussions around the invariant that names(x)[j] should be the same as names(x[j]) . We could run revdepchecks to see what the impact would be.?

krlmlr avatar Sep 24 '22 14:09 krlmlr

I actually think this behavior is good and correct. I think [ is a suitably low level operation that it shouldn't apply automatic name repair because it is going to be used programmatically.

I know of at least 1 place where we use [ in a way that might generate names like this, but it works just fine anyways.

join_by(x >= lower, x <= upper) in dplyr will generate key data frames that have two columns where both columns are named x (because names come from the lhs input). These are essentially only used by vec_locate_matches() so the duplicate names don't hurt anything.

DavisVaughan avatar Sep 25 '22 13:09 DavisVaughan

Agree to close?

krlmlr avatar Dec 04 '22 19:12 krlmlr