Reputation: 153
In a folder I have like 24 *.csv files, they have the same structure and I want to create a single file. The issue is that not all the data frame are read in the same way, because for some files the sep parameter is "," and to the others ";". What approach should I use ? I guess probably I might put an if-statement.
Upvotes: 1
Views: 434
Reputation: 153
The following simple code did the work, and very fast.
library(dplyr)
library(data.table)
files <- list.files(path = "Ariel/Trips/", full.names = T)
f <- list()
for (i in 1:length(files)) {
f[[i]] <- fread(files[i], header = T, colClasses = c("factor", "factor", "factor", "factor",
"factor", "factor", "factor", "factor",
"factor", "factor", "factor", "factor",
"factor", "integer", "integer", "integer",
"factor", "numeric", "numeric", "integer",
"factor", "factor", "factor", "factor",
"factor", "factor", "factor", "factor"))
}
trip <- bind_rows(f)
fwrite(x = trip, file = "trip.csv", quote = T, na = NA, row.names = F)
Upvotes: 1
Reputation: 2228
You can check the first line of the data frame to see if it has comma or semicolon as separator, and after call an ifelse statement:
L <- readLines('DataFrame', n = 1)
if (grepl(";", L)) read.csv2("DataFrame") else read.csv("DataFrame")
Upvotes: 2