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

Using a named vector with dplyr::select(dplyr::all_of(...)) changes the table column names #1447

Closed
andreassoteriadesmoj opened this issue Feb 8, 2024 · 1 comment

Comments

@andreassoteriadesmoj
Copy link

andreassoteriadesmoj commented Feb 8, 2024

When passing a named vector in dplyr::select(dplyr::all_of(...)), the names of the table fields to select are replaced by the names of the vector elements containing the name of the fields. Is this a bug?

I'm using dbplyr 2.4.0

iris_db <- iris %>% 
  dbplyr::memdb_frame()

# Named vector #
species <- c(sp = 'Species')
# Expecting 'Species', getting 'sp' (= names(species))
iris_db |>
  dplyr::select(
    dplyr::all_of(
      species
    )
  ) |> 
  colnames()

# [1] "sp"

# Not named vector #
species_noname <- 'Species'
# Expecting 'Species', getting 'Species' (!= names(species))
iris_db |>
  dplyr::select(
    dplyr::all_of(
      species_noname
    )
  ) |> 
  colnames()

# [1] "Species"
@hadley
Copy link
Member

hadley commented Feb 14, 2024

This is by design of dplyr:

library(dplyr, warn.conflicts = FALSE)


species <- c(sp = 'Species')
iris |> select(all_of(species)) |> colnames()
#> [1] "sp"
iris |> rename(all_of(species)) |> colnames()
#> [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "sp"

Created on 2024-02-14 with reprex v2.0.2.9000

@hadley hadley closed this as completed Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants