0

I'm trying to read multiple csv files and store them in 1 dataframe. What I did is:

 files <- list.files(path="the path/", pattern="*.csv")

 df = lapply(files, read.csv,sep = ";", encoding = "ISO-8859-1")

I've tried also sapply.

I'm getting:

Error in file(file, "rt") : cannot open the connection cannot open file 'onefile.csv': No such file or directory

Cœur
  • 37,241
  • 25
  • 195
  • 267
datascana
  • 641
  • 2
  • 8
  • 16

1 Answers1

3

data.table has a fast way to rbind a list of data frames (which is what the lapply returns), so using rbindlist:

library(data.table)
files <- list.files(path="the path/", pattern="*.csv", full.names = TRUE)
dt <- rbindlist(lapply(files, read.csv,sep = ";", encoding = "ISO-8859-1"),
  use.names = TRUE, fill = TRUE)

To use fread from data.table in the lapply:

dt <- rbindlist(lapply(files, fread, sep = ";", encoding = "Latin-1"),
  use.names = TRUE, fill = TRUE)

To do this in base R (without using rbindlist or fread from data.table):

df <- do.call(rbind, lapply(files, read.csv, sep = ";", encoding = "ISO-8859-1"))
  • Yes, fread is much faster. The encoding needs to be changed to `Latin-1` though. – Kristoffer Winther Balling Apr 20 '17 at 09:38
  • I'm new to R, is it normal that the print not showing all the rows and 1 csv ? – datascana Apr 20 '17 at 09:40
  • By default `data.table` only shows the head and tails of the data if there are more than 100 rows. You can change that as described [here](http://stackoverflow.com/questions/12162657/possible-to-print-more-than-100-rows-of-a-data-table), or convert `dt` to a data frame using `setDF(dt)`, or use the base R method I added to the answer (will result in a standard data frame). – Kristoffer Winther Balling Apr 20 '17 at 09:46