How do I read a variable number of files and then concatenate the data in R?
I would like to create a function. Let's say I have files file1.csv, file2.csv, file3.csv, ..., file100.csv. I only want to read some of them every time I call the function giving an integer vector id like id = 1:10, then I will read file1.csv, ..., file10.csv.
After reading these csv files, I would like them to combine them into one variable. All csv files have the same column structure.
My code is below:
namelist <- list.files()
for (i in id) {
assign(paste0( "file", i ), read.csv(namelist[i], header=T))
}
As you can see, after reading the entire data matrix, I stuck with a combination of them since they all have different variable names.
source to share
You must read in each file as a list item. Then you can combine them like this:
namelist <- list.files()
df <- vector("list", length = length(id))
for (i in id) {
df[[i]] <- read.csv(namelist[i], header = TRUE)
}
df <- do.call("rbind", df)
Or more succinctly:
df <- do.call(rbind, lapply(list.files(), read.csv))
source to share
If I understand correctly what you want to do, this is all you need:
namelist <- list.files()
singlevar = c()
for (i in id) {
singlevar = rbind(singlevar, read.csv(namelist[i], header=T))
}
Since at the end you want one single object to contain all the partial information from the individual files rbind
, as you go.
source to share