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

read_csv: Simultaneous selection and renaming of columns #1404

Open
nbsmokee opened this issue May 25, 2022 · 0 comments
Open

read_csv: Simultaneous selection and renaming of columns #1404

nbsmokee opened this issue May 25, 2022 · 0 comments
Labels
feature a feature request or enhancement read 📖

Comments

@nbsmokee
Copy link

When reading data with read_csv (and probably other read_* functions), I sometimes need to read only some columns and also rename them. When I try to do this with one call to read_csv, I would expect that when I use both the col_select and col_names keywords, I have to provide only new names for the selected columns, but instead, in order for the function to produce the desired output, I have to provide names for all columns in the file, which is somewhat annoying, at least in my particular use case.

In the below example, I think it is much more natural to use the 1. test case, however the result I expect is produced only by the 2. function call, the 1st one giving an error.

library("readr")

# Create test file
readr::write_csv(data.frame(array(1:24, c(6, 4))), "test.csv")

# TEST CASES:
# 1. Attempt (and fail) to select AND remane simultaneously
readr::read_csv("test.csv", col_select = 2:3, skip = 1, col_names = paste0("col", 2:3))

# 2. Working (but unpleasant) solution: provide new names for ALL columns
readr::read_csv("test.csv", col_select = 2:3, skip = 1, col_names = paste0("col", 1:4))
@sbearrows sbearrows self-assigned this Aug 25, 2022
@hadley hadley added feature a feature request or enhancement read 📖 and removed collector labels Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement read 📖
Projects
None yet
Development

No branches or pull requests

3 participants