Arik Ryvkin
Arik Ryvkin

Reputation: 21

Apply a raw parameter from one data frame to another using plyr package

I want to take the following index data frame:

sample   label                            Path
1       B6_01      B6   zap17KO_data/B6_1_May2015.tsv
2       B6_02      B6   zap17KO_data/B6_2_May2015.tsv
3       B6_03      B6   zap17KO_data/B6_3_May2015.tsv
4     Quad_01    Quad zap17KO_data/Quad_1_May2015.tsv
5     Quad_02    Quad zap17KO_data/Quad_2_May2015.tsv
6     Quad_03    Quad zap17KO_data/Quad_3_May2015.tsv
7  ZAPKODN_01 ZAPKODN      zap17KO_data/ZAPKODN_1.tsv
8  ZAPKODP_01 ZAPKODP      zap17KO_data/ZAPKODP_1.tsv
9  ZAPKODP_02 ZAPKODP      zap17KO_data/ZAPKODP_2.tsv
10 ZAPKODP_03 ZAPKODP      zap17KO_data/ZAPKODP_3.tsv

And to read each of the files in the paths to a single data frame and that for each raw of the new data frame I will have the concordant sample and label.

I tried to do it by reading the index data frame to a list:

flist <- index$paths
names(flist) <- index$sample
db <- ldply(flist, read.delim)

The problem here is that I don't have the label for each raw in the db data frame and I need to use "match" function to get it right from which I want to abstain.

Upvotes: 0

Views: 43

Answers (2)

Arik Ryvkin
Arik Ryvkin

Reputation: 21

As I was looking for a solution with the plyr package i found this to work:

db <- ddply(index, .(Path), function(x){
  df <- read.delim (x$Path)
  df$.id <- x$sample
  df$label <- x$label
  return(df)
})

Upvotes: 0

zx8754
zx8754

Reputation: 56149

Something like this:

res <- 
  do.call(rbind,
          lapply(1:nrow(index),function(i){
            myPath <- index[i, "Path"]
            mySample <- index[i, "sample"]
            myLabel <- index[i, "label"]

            #read tsv
            d <- read.table(myPath, sep = "\t")

            #result
            cbind(mySample, myLable, d)
          }
          ))

Upvotes: 1

Related Questions