3

I have a folder with hundreds of .csv files that I would like to merge into a single data frame.

I've been using this code which works relatively well but only for files with matching headers.

    df <- do.call(rbind, lapply(filenames , read.csv))

Two Part Question.

1.) I would like to append this do.call code to include an additional column that includes the .csv file name for each row of data

2.) I would also like to be able to merge .csv files with non-matching headers. Each file has four columns (with different names) but the data type matches. So I want to force the files to bind together regardless of column header.

This post has been helpful but not for merging files with non-mathcing headers:

Merge multiple CSV files and remove duplicates in R

I've also used this code; again only for files with matching headers:

    for (file in filenames){
   # if the merged dataset doesn't exist, create it
    if (!exists("dataset")){
     dataset <- read.csv(file, header=TRUE, sep="\t")
    }
   # if the merged dataset does exist, append to it
    if (exists("dataset")){
     temp_dataset <-read.csv(file, header=TRUE, sep="\t")
     dataset<-rbind(dataset, temp_dataset)
     rm(temp_dataset)
    }
   }
4

1 回答 1

0

What about:

df <- do.call(rbind, lapply(filenames , 
                        function(x) data.frame(filname = x, read.csv(x)))
于 2013-10-03T18:59:01.930 回答