0

I have 12 csv files in one folder, and try to import combine it into 1 single file. Currently i use this code:

    allFileNames <- list.files("AllData")

for (filename in allFileNames) { 
  fullFilename <- paste0("AllData/", filename)
  data <- read.csv(fullFilename,  
                          skip=2,  
                          na.strings="<1") 

  data[is.na(data)] <- 0 
  names(data) <- c("x","y","z")
  data <- rbind(data,deparse.level = 0,make.row.names = TRUE,
                   stringsAsFactors = default.stringsAsFactors(),
                   factor.exclude = NA)

}

But it only shows 1 file instead of combine all 12 files. In which part did i do wrong?

aua
  • 57
  • 7
  • Do you want [this](https://stackoverflow.com/a/46837941/8245406)? Or [this one](https://stackoverflow.com/a/44330790/8245406)? – Rui Barradas Feb 14 '20 at 12:34

1 Answers1

0

You are intializing data each iteration and replacing whatever was stored there in the beginning. You need to initialize data before the loop, store the temporary file in a new variable then rbind it.

user2974951
  • 4,352
  • 1
  • 11
  • 18