Skip to content

relocate.data.frame() shouldn't rely on vec_unique() retaining names #6209

@DavisVaughan

Description

@DavisVaughan

Currently, relocate() relies on the fact that vec_unique() keeps names

dplyr/R/relocate.R

Lines 83 to 85 in 8abb54b

pos <- vec_unique(c(lhs, to_move, rhs))
out <- .data[pos]
new_names <- names(pos)

We think we should actually drop names from the result of vec_unique(), since the returned names are reliant on the fact that only the first encountered duplicate value is returned (this is also consistent with unique()). See r-lib/vctrs#1442 and r-lib/vctrs#1532 where we tried to implement this, but found that dplyr relies on this behavior currently.


One easy way to fix this is to use vec_unique_loc() to get the locations, and then vec_slice(), which will retain names.

That should work in the immediate term but it may be worth looking at the code to also see why we currently get names off the unique values, and if there is something else we should be doing instead, since that feels unreliable.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions