I have several different folders which all contain one single .csv file. All of those .csv files have one single column containing the data of one condition of an experiment. I would like to merge those .csv files in such a way that the data of every file is added as a new column.
At the moment, it Looks somehow like this:
C1.csv
102
106
152
196
223
486
553
C2.csv
296
299
843
1033
1996
However, it would like to have one single .csv file, where all the separate files are copied into a new column containing the name of the source file, like:
C1 C2 ... Cn
102 296 ... ...
106 299 ...
152 843 ...
196 1033 ...
223 1996 ...
486 ...
553 ...
So far, I the following code:
myFiles = list.files(path = ".", recursive = TRUE, pattern = ".csv", full.names = TRUE)
data <- lapply(myFiles, read.table, sep="\t", header=FALSE)
Max <- max(sapply(data, length))
data <- lapply(data, function(x) c(x, rep(NA, Max - length(x))))
data <- do.call(cbind, data)
names(data) <- sub("^[^[:alnum:]]*([[:alnum:]]+)\\.csv$", "\\1", myFiles)
write.csv(data, "outfile.csv")
It yielded a document that looks like this instead of adding the data of every .csv file in a new column:
One can read all files using
read.table
in a list. Combine all data usingdplyr::bind_rows
. Afterwards, usereshape2::dcast
to spread data in wide format with a column for data from every file.Is this what you want?
Note that I read the files in with
scan
. Since the files have only one column there is no need for a complex function likeread.csv
.The contents of
"outfile.csv"
are