cbind with row names to spliting by Chr in R
0
1
Entering edit mode
10.6 years ago
viniciushs88 ▴ 50

I would like to use the cbind in a list of files. However each file are split 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)}
}

In the end I just get the 3 columns representing my row names and just one column with values (4 column in all sample input files) Cheers!

split merge R cbind chr • 3.9k views
ADD COMMENT
0
Entering edit mode

I doubt that all your files have identical row-names because the chromosomes are different.

ADD REPLY
0
Entering edit mode

All sample files to a same Chr have. In the end I expect 29 final files (one per Chr). I wanna o ´cbind´ all values (4 collum in input files), separately to each Chr.

I have 22553 diferent .txt files (29 files (one per chr) to each of 777 samples).

ADD REPLY
0
Entering edit mode

This is really not a good example of a problem that R is designed for. Is there a reason you don't want to use python or perl or even C (all of which would perform vastly better)?

ADD REPLY
0
Entering edit mode

If this is really your code maybe you just miss one pair of parentheses around the inner for loop?

ADD REPLY

Login before adding your answer.

Traffic: 1927 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6