Question: cbind with row names to spliting by Chr in R
1
gravatar for viniciushs88
3.4 years ago by
viniciushs8850
Germany
viniciushs8850 wrote:

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 collumns 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 collumns representing my rows names).

I tried this:

`#Creating file with row names (3 first collumns) 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 collumns representing my row names and just one collumn with values (4 collumn in all sample input files) Cheers!

cbind split chr merge R • 1.5k views
ADD COMMENTlink modified 3.4 years ago • written 3.4 years ago by viniciushs8850

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

ADD REPLYlink written 3.4 years ago by Michael Dondrup43k

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 REPLYlink modified 3.4 years ago • written 3.4 years ago by viniciushs8850

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 REPLYlink written 3.4 years ago by Devon Ryan70k

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

ADD REPLYlink modified 3.4 years ago • written 3.4 years ago by Ido Tamir4.7k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 684 users visited in the last hour