I would like to use the cbind
in a list of files. However each file are splited in a specific chromosome (chr) (k in 1:29)
, and specific sample (i in 1:777)
. The files are like:
sample1chr1.txt, sample1chr2.txt ... sample1chr29.txt, sample2chr1.txt ... sample777chr29.txt
All files have exactly the same rows names (3 first columns represent my row names). I would like to get a final file to each chr merging to all sample files, with and do not repeat the row names in the final file (the first 3 columns representing my rows names).
I tried this:
#Creating file with row names (3 first columns) to each Chr
{
{for(k in 1:29){
infile <- paste0("sample1chr",k,".txt")
outfile <- paste0("LRRrawallchr",k,".txt")
rows <- read.table(infile, header=TRUE, sep="\t")
rows <- rows[, -grep("Log.R.Ratio", colnames(rows))]
write.table(rows, outfile, sep=";")}}
#Cbind in one file per Chr
{ for(i in 1:777)
for(k in 1:29){
base <- paste0("LRRrawallchr",k,".txt")
chr <- read.table(base, header=TRUE, sep=";")
infile <- paste0("sample",i,"chr",k,".txt")
chr2 <- read.table(infile, header=TRUE, sep="\t")
outfile <- paste0("LRRrawallchr",k,".txt")
chr2 <- chr2[, -grep("Name", colnames(chr2))]
chr2 <- chr2[, -grep("Chr", colnames(chr2))]
chr2 <- chr2[, -grep("Position", colnames(chr2))]
chr <- cbind(chr, chr2)
write.table(chr, outfile, sep=";", row.names=FALSE, col.names=FALSE)}
}
Input example (sample1chr1.txt):
Name Chr Position sample1value
BAC-11034 1 128 0.302
BAC-11044 1 129 -0.56
BAC-11057 1 134 0.0840
Input example (sample2chr1.txt):
Name Chr Position sample2value
BAC-11034 1 128 0.25
BAC-11044 1 129 0.41
BAC-11057 1 134 -0.14
Expected output (LRRrawallchr1):
Name Chr Position sample1value sample2value
BAC-11034 1 128 0.302 0.25
BAC-11044 1 129 -0.56 0.41
BAC-11057 1 134 0.0840 -0.14
I have 22553 different .txt files (29 files (one per chr) to each of 777 samples). All 22553 files (sample1chr1.txt, sample1chr2.txt ... sample1chr29.txt, sample2chr1.txt ... sample777chr29.txt) are like above example.
I wanna 29 files like (LRRrawallchr1), one per Chr. The "LRRrawallchr,k," files have to be with 777+3 (800 collumns). The 3 row names and one column per sample.
Cheers!