Apologies if this has been answered else where. I'm new to R, and have spent all of my 2 days using it trying to get past this initial hurdle.
I've been given a data set with approximately 2000 separate data files. I would like to merge them in to one very large data set. I've found a couple of ways that people suggest work, but none have worked for me. For example, one blog (http://psychwire.wordpress.com/2011/06/03/merge-all-files-in-a-directory-using-r-into-a-single-dataframe/) recommends using the following code:
setwd("target_dir/")
file_list <- list.files()
for (file in file_list){
# if the merged dataset doesn't exist, create it
if (!exists("dataset")){
dataset <- read.table(file, header=TRUE, sep="\t")
}
# if the merged dataset does exist, append to it
if (exists("dataset")){
temp_dataset <-read.table(file, header=TRUE, sep="\t")
dataset<-rbind(dataset, temp_dataset)
rm(temp_dataset)
}
}
When I use this code (changing 'target_dir' to the correct directory), R presents me with the following:
Error in match.names(clabs, names(xi)) :
names do not match previous names
My hunch is that I've either not changed one of the variables within the code which I need to so that it relates to my specific data (I changed the 'target_dir' to the correct directory, but didn't change anything else), or it is because the .dat files don't have any column headings. If this is the case, my second question is whether there is a way of creating the same column headings for multiple .dat files using R.
Many thanks for taking the time to read this.