[R] cbind with row names to serveral files in R

arun smartpink111 at yahoo.com
Sat Apr 19 19:40:49 CEST 2014

The rownames part is not clear as your expected output and input files didn't show them as rownames. 

##Suppose you have all the files in a folder
##here I am creating the names of those files

files1 <- paste0("sample", rep(1:777, each=29),"chr",1:29,".txt")
#[1] 22533
lst1 <-  split(files1,as.numeric(gsub(".*chr(\\d+)\\..*","\\1",files1)))

##use list.files() 

##in your case, ##not tested

lst1 <- split(list.files(pattern="sample\\d+"), as.numeric(gsub(".*chr(\\d+)\\..*","\\1",list.files(pattern="sample\\d+")))) ##in case other files are also in the folder


lst2 <- lapply(lst1,function(x) {x1 <-join_all(lapply(x,function(y) read.table(y,header=TRUE,stringsAsFactors=FALSE,sep="")),c("Name","Chr","Position")) })

lapply(seq_along(lst2),function(i) write.table(lst2[[i]],paste0("LRRrawallchr",i,".txt"),row.names=FALSE,quote=FALSE))

###if you need to create rownames using the first three columns:
lapply(seq_along(lst2),function(i) {x1 <- lst2[[i]]; row.names(x1) <- as.character(interaction(x1[,1:3],sep="_")); x2 <- x1[,-(1:3)]; write.table(x2, paste0("LRRrawallchr",i,".txt"), quote=FALSE)})


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)}

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 diferent .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 collumn per sample.


More information about the R-help mailing list