Aggregate data from different files into a data structure

I noticed that I often come across this problem when programming in R, but I don’t think that I am implementing it “enough”.

I get a list of file names, each of which contains a table or a simple vector. I want to read all the files in some kind of construction (a list of tables?), So that later I could manipulate them in simple loops.

I know how to read each file in a table / vector, but I do not know how to combine all these objects into one structure (list?).

In any case, I think this is a VERY routine, so I will be happy to hear about your tricks.

+4
source share
3 answers

Or, without plyr :

 filenames <- c("file1.txt", "file2.txt", "file3.txt") mydata <- array(list(NULL)) for (i in 1:length(filenames)) { mydata[[i]] <- read.table(filenames[i]) } 
+2
source

Do all files have the same number of columns? If so, I think it should work to put them all in one data frame.

 library(plyr) x <- c(FILENAMES) df <- ldply(x, read.table, sep = "\t", header = T) 

If they do not have the same columns, use llply() instead

+3
source

All Articles