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

Error about 4 columns even though am only asking for 3 #1472

Closed
ggrothendieck opened this issue Feb 22, 2023 · 1 comment
Closed

Error about 4 columns even though am only asking for 3 #1472

ggrothendieck opened this issue Feb 22, 2023 · 1 comment

Comments

@ggrothendieck
Copy link

If we run read_csv asking for 3 columns it gives an error if any of the files have more. I would have expected it to work by simply ignoring the columns that are not asked for.

library(readr)
packageVersion("readr")
## [1] ‘2.1.4’

fileNames <- c("test1.csv", "test2.csv")
cat("a,b,c\n1,2,3", file = fileNames[1])
cat("a,b,c,d\n11,12,13,14", file = fileNames[2])

read_csv(fileNames, col_select = 1:3, skip = 1, col_names = c("A", "B", "C"), id = "id")
## Error: Files must all have 3 columns:
## * File 2 has 4 columns
@hadley
Copy link
Member

hadley commented Jul 31, 2023

That's because the column specification has to be identical when writing multiple files. If you want to handle heterogenous files, we recommend using purrr or similar, e.g. https://r4ds.hadley.nz/iteration.html#heterogeneous-data.

@hadley hadley closed this as completed Jul 31, 2023
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