0

I have 7 files in my directory, I am trying to import all of them dynamically. I am using the following code:

file <- dir(getwd(),full.names=TRUE)    
for (i in 1:length(file)){
  file_i <- read.csv(file[i],stringsAsFactors = F)
}         

Above code is not working as we have to define the new variable outside the for loop. Can anybody suggest me a way?

pranav
  • 1,041
  • 1
  • 10
  • 11
  • 4
    Probably just `files <- lapply(file, read.csv, stringsAsFactors = FALSE)` (untested). Then you'll have a list with all the files which you can select data sets from or collapse it into one data set (if all have same column names) using something like `DF <- do.call(rbind, files)` – David Arenburg Aug 25 '15 at 11:26
  • Or we can read the files with `fread` from `data.table`. `library(data.table); files <- lapply(file, fread)` and then convert to a single dataset if needed `DT <- rbindlist(fread, idcol=TRUE)`. – akrun Aug 25 '15 at 11:28
  • Looks like a duplicate of http://stackoverflow.com/questions/11433432/importing-multiple-csv-files-into-r . – maj Aug 25 '15 at 11:45
  • @maj Your suggestion already has been taken into account. –  Aug 25 '15 at 11:47

0 Answers0