0

I have a list called "unique_tissues" of vectors where each vector (element in the list) represents one tissue type and the elements of the vectors are sample names.

$liver
 [1] "liver_10a_P1973_515.tsv" "liver_10b_P1973_519.tsv" "liver_10c_P1973_520.tsv" "liver_9a_P1263_202.tsv"  "liver_9b_P1263_210.tsv"  "liver_9c_P1263_218.tsv" 
 [7] "liver_9d_P1263_226.tsv"  "liver_a_P000_023.tsv"    "liver_c_P000_025.tsv"    "liver_d_P000_026.tsv"   

$lung
[1] "lung_10a_P1973_510.tsv" "lung_11a_P2952_102.tsv" "lung_11c_P2952_104.tsv" "lung_11e_P2952_106.tsv" "lung_3e_P262_148.tsv"   "lung_3f_P262_149.tsv"   "lung_4a_P282_111.tsv"  
[8] "lung_4b_P282_112.tsv"   "lung_4d_P282_114.tsv"  

$prostate
 [1] "prostate_10a_P1973_502.tsv" "prostate_10b_P1973_503.tsv" "prostate_10c_P1973_504.tsv" "prostate_10d_P1973_507.tsv" "prostate_10f_P1973_506.tsv" "prostate_4a_P282_108.tsv"  
 [7] "prostate_4b_P282_109.tsv"   "prostate_4c_P282_110.tsv"   "prostate_9b_P1263_242.tsv"  "prostate_9d_P1263_301.tsv"  "prostate_a_P189_111.tsv"   

The files all look the same and I would like to download all files and merge them into one data frame per tissue type.

all files are present in a folder named "directory_t" and the files all share the same format with the two columns "target_id" and "TPM".

I have tried to use:

 tissue_data <- lapply(unique_tissues, function(x) {

    a <- read.table(paste0(directory_t,"/",x[1]), stringsAsFactors = FALSE,header=T)[,c(1,5)] 

    for(i in 2:length(x)){
      b <- read.table(paste0(directory_t,"/",x[i]), stringsAsFactors = FALSE,header=T)[,c(1,5)]
      a <- merge(a,b,by="target_id")

    }

    return(a)
  })

Is there a way to do this in a better way?

Frank
  • 66,179
  • 8
  • 96
  • 180
user3346285
  • 101
  • 1
  • 1
  • 5
  • 2
    Yes, `lapply(filenames, read.table, stringsAsFactors = FALSE,header=T)` and then `Reduce` in conjunction with `merge`. – Roland Jan 15 '16 at 15:23
  • You can try `lapply(unique_tissues, function(x) {lst <- lapply(x, function(y) {a <- read.table(.....)); Reduce(function(...) merge(..., by = "target_id"), lst)})` – akrun Jan 15 '16 at 15:23
  • [This discussion](http://stackoverflow.com/a/8097519/1305688) might be helpful. – Eric Fail Jan 15 '16 at 15:24
  • You sure you want to `merge`? If the "files all look the same", you probably want to append/stack/`rbind` them on top of each other. – Frank Jan 15 '16 at 15:45

0 Answers0