开发者

Aggregate data from different files into data structure

开发者 https://www.devze.com 2023-01-10 07:10 出处:网络
I noticed I encounter this task quite ofte开发者_开发问答n when programming in R, yet I don\'t think I implement it \"pretty\".

I noticed I encounter this task quite ofte开发者_开发问答n when programming in R, yet I don't think I implement it "pretty".

I get a list of file names, each containing a table or a simple vector. I want to read all the files into some construct (list of tables?) so I can later manipulate them in simple loops.

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

Anyway, I guess this is VERY routine so I'll be happy to hear about your tricks.


Do all the files have the same # of columns? If so, I think this should work to put them all into one dataframe.

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

If they don't have all the same columns, then use llply() instead


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])
    }


You can have a look at my answer here: Merge several data.frames into one data.frame with a loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消