elixir elixir - 16 days ago 7
R Question

How to read variable number of files and then combine the data frames in R?

I would like to design a function. 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 by specifying an integer vector id, e.g., id = 1:10, then I will read file1.csv,...,file10.csv.

After reading those csv files, I would like to row combine them into a single 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 I read in all the data matrix, I stuck at combining them since they all have different variable names.

Answer

You should read in each file as an element of a list. Then you can combine them as follows:

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 concisely:

df <- do.call(rbind, lapply(list.files(), read.csv))
Comments