R reading multiple files, concatenating results -


nvar    6957423 rate    1 mac 963.605 maf 0.228126 sing    0 mono    0 titv    1.99326 titv_s  na dp  na qual    na pass    1 filter|pass 1 pass_s  0 

i have several files (n=414) format above. in r, read files, transpose, , rbind or concatenate values. files named age_wg2_ind1.vstats, ranging 1 414 (after ind[i].vstats). far, i've tried this:

txtfiles = list.files(pattern="*.vstats")  (i in 1:length(txtfiles)){      tmp = read.table(txtfiles[i],sep="\t")   ttmp<-t(tmp[i])  colnames(ttmp)<-ttmp[1,];ttmp2<-ttmp[2:nrow(ttmp),] } 

error in ttmp[2:nrow(ttmp), ] : subscript out of bounds

1) list files command begin individual #1 , end #414? 2) not sure put [i] retain second row of each file.

thanks!

if files have variable names in first column values in second following should work

l <- lapply(txtfiles , function(i) {                   r <- read.table(i ,sep="\t")                  mat <- t(r[,2])                  colnames(mat) <- r[,1]                  mat                  })  (out <- do.call(rbind , l)) 

if have (some) different variable names in each file have @ rbind.fill in plyr package

you can @ order of files in txtfiles - numerical order of files not preserved. can pre-process order of txtfiles before lapply loop or files named define file lists

txtfiles2 <- paste0("age_wg2_ind",1:414,".vstats") 

edit

guessing error input error 1 of files - can try this. have made data o show working. if run previous code above (on example data below)you error message 'error in read.table(i, sep = "\t") : no lines available in input'. using trycatch woks.

#some example data  df <- data.frame(letters[1:3] , 1:3) write.table(df,"temp1.out",sep="\t" , row.names=f , col.names=f) write.table(df,"temp2.out",sep="\t", row.names=f , col.names=f) df[,1] <- df[,2] <- null write.table(df,"temp3.out",sep="\t", row.names=f , col.names=f) # 0 columns  #read in data txtfiles <- list.files(pattern="*.out")  l <- lapply(txtfiles , function(i) {                  r <-  trycatch(read.table(i , sep="\t"), error=function(e) null)                  if(!is.null(r)) {                      mat <- t(r[,2])                      colnames(mat) <- r[,1]                      mat         }})  l <- l[!sapply(l, is.null)]  (out <- do.call(rbind , l)) 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -