Shashank Walake
Shashank Walake

Reputation: 47

Merging csv files in R

I have around 5000 csv files of small sizes. each one has min 4 columns and maximum 7 columns. I want to merge them all either on single spreadsheet or different spreadsheets in one work book. I've referred Trying to merge multiple csv files in R at instance. 1st answer seemed to work but console gave me error massage as follows:

Error: Column V3 can't be converted from character to numeric In addition: There were 50 or more warnings (use warnings() to see the first 50)

The thing that df is default function in R. So, i assigned value as a<- list.files(.........) Where am I going wrong. Thanks for the Help in advance.

Upvotes: 1

Views: 301

Answers (1)

linog
linog

Reputation: 6246

It's because in some cases V3 column is interpreted as character while in others it is interpreted as numeric (readr::read_csv tries a guess of your datatype to speed up import).

So, you can try the following

library(dplyr)
library(readr)

df <- list.files(path="yourpath", full.names = TRUE) %>% 
  lapply(read_csv,  col_types = cols( .default = col_character())) ) %>% 
  bind_rows 

You will import all columns as characters there, at the cost of performance (and the possible need to change column types later)

The equivalent data.table implementation (should be faster) is :

library(data.table)

df <- lapply(list.files(path="yourpath", full.names = TRUE), fread)
df <- rbindlist(df, use.names = TRUE, fill = TRUE)

Upvotes: 2

Related Questions