Bhaskar Mitra
2022-Jun-01 23:17 UTC
[R] Request for some help: Error when joining multiple csv files together
Hello Everyone,
I have a bunch of csv files, all with common headers (d1, d2, d3, d4).
When I am trying to join the csv files together, with the following code
(shown below),
I am getting a warning when the files are joined. The values under columns
d3 and d4 are not joined properly.
The code and the error are given below. I would really appreciate some help
in this regard.
regards,
bhaskar
#---code--------------------------------
df <- list.files(pattern = "*.csv") %>%
lapply(read_csv) %>%
bind_rows
#---code--------------------------------
Header of each file:
d1 d2 d3 d4
3 4 NA NA
4 5 NA 7
5 6 8 8
6 7 8 NA
#--------------------------------------------------------
#Warning when the codes run --------------------------------------
Column specification
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????
cols(
.default = col_double(),
name = col_character(),
d1 = col_datetime(format = ""),
d2 = col_character(),
d3 = col_logical(),
d4 = col_logical()
)
Warning: 48766 parsing failures.
* row* *col* * expected * *actual*
*file*
3529 d3 1/0/T/F/TRUE/FALSE 100 'file1.csv'
3529 d4 1/0/T/F/TRUE/FALSE 100 'file1.csv'
.... ..... .................. ...... ................................
[[alternative HTML version deleted]]
Rui Barradas
2022-Jun-02 07:30 UTC
[R] Request for some help: Error when joining multiple csv files together
Hello, I'm seeing two obvious errors, those are not csv files and the columns spec is wrong, you have a spec of 6 columns but the posted data only has 4 and of different classes. If the data is in comma separated values (CSV) files the following worked without errors. library(readr) col_spec <- cols( d1 = col_double(), d2 = col_double(), d3 = col_double(), d4 = col_double() ) list.files(pattern = "*\\.csv$") |> lapply(read_csv, col_types = col_spec) |> dplyr::bind_rows() If the data is like in the question, try instead list.files(pattern = "*\\.csv$") |> lapply(read_delim, delim = " ", col_types = col_spec) |> dplyr::bind_rows() Hope this helps, Rui Barradas ?s 00:17 de 02/06/2022, Bhaskar Mitra escreveu:> Hello Everyone, > > I have a bunch of csv files, all with common headers (d1, d2, d3, d4). > When I am trying to join the csv files together, with the following code > (shown below), > I am getting a warning when the files are joined. The values under columns > d3 and d4 are not joined properly. > > The code and the error are given below. I would really appreciate some help > in this regard. > > regards, > bhaskar > > > #---code-------------------------------- > > df <- list.files(pattern = "*.csv") %>% > lapply(read_csv) %>% > bind_rows > > #---code-------------------------------- > > Header of each file: > > d1 d2 d3 d4 > 3 4 NA NA > 4 5 NA 7 > 5 6 8 8 > 6 7 8 NA > > #-------------------------------------------------------- > > #Warning when the codes run -------------------------------------- > > Column specification > ????????????????????????????????????????????????????????????????????????????????????????????????????????????????? > cols( > .default = col_double(), > name = col_character(), > d1 = col_datetime(format = ""), > d2 = col_character(), > d3 = col_logical(), > d4 = col_logical() > ) > > Warning: 48766 parsing failures. > * row* *col* * expected * *actual* > *file* > 3529 d3 1/0/T/F/TRUE/FALSE 100 'file1.csv' > 3529 d4 1/0/T/F/TRUE/FALSE 100 'file1.csv' > > .... ..... .................. ...... ................................ > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.