Dr. Moustachio
Dr. Moustachio

Reputation: 3

Merge mutiple .cbs files into one data frame that includes the file names in a new first column

I am working with a large number of .cbs files in R. I have found a useful bit of code here (How do you read in multiple .txt files into R? user Greg's response) which I changed from .txt to .cbs:

Following three lines takes all the files and makes one data.frame "datafr" with all lines of code in:

filelist = list.files(pattern = ".*.cbs")

Assuming tab separated values with a header:

datalist = lapply(filelist, function(x)read.table(x, header=T)) 

Assuming the same header/columns for all files:

datafr = do.call("rbind", datalist)

This works really well for merging the files into one data.frame, however I would like to also include the name of the file that each line in the new merged data frame came from orignially. Ideally as a new first column of the data.frame. Can anyone please help me adapt this code?

Thanks,

John

Upvotes: 0

Views: 49

Answers (2)

Parfait
Parfait

Reputation: 107687

Consider transform:

datalist = lapply(filelist, function(x) transform(read.table(x, header=T), filename=x)) 

Upvotes: 0

Rui Barradas
Rui Barradas

Reputation: 76575

Maybe the following will do. Untested, since there is no data example.

datalist = lapply(seq_along(datalist), function(i){
        datalist[[i]]["filename"] <- filelist[[i]]
        datalist[[i]]
    }) 

I should also mention that I, and many others, prefer the assignment operator <- over =.

Upvotes: 1

Related Questions