I have a 'homework' to generate a matrix which records some matched information. I know I can easily figure this out by for-loop but I really have a rather big matrix which shall end up with endless waiting. I wish if there is a faster way to get there.
Basically, what I want to do is to find a matched gene expression and pathway. To be specific, if a gene (e.g., gene 1) belongs to a pathway (e.g., pathway1), then give the corresponding expression to the gene1-pathway1 combination. However, if the gene is not in this pathway, 0 is assigned. See, it is simple but I am stuck. Please see the following example to express what I want to get.
path <- read.table(header = T,text = "pathway   gene
pathway1    gene1
           pathway1 gene2
           pathway1 gene3
           pathway1 gene4
           pathway2 gene1
           pathway2 gene5
           pathway3 gene3
           pathway3 gene6
           pathway3 gene7
           ")
expr <- read.table(header = T,text = "gene  expression
gene1   1
gene2   2
gene3   3
gene4   4
gene5   5
gene6   6
gene8 8
")
out <- matrix(0,
              nrow = length(unique(path$pathway)),
              ncol = length(unique(expr$gene)),
              dimnames = list(unique(path$pathway),unique(expr$gene)))
for (p in rownames(out)) {
  for (g in colnames(out)) {
    tmp <- path[which(path$pathway == p),]
    if(is.element(g,tmp$gene)) {
      out[p,g] <- expr[which(expr$gene == g),"expression"]
    } else {next()}
  }
}
print(out)
#          gene1 gene2 gene3 gene4 gene5 gene6 gene8
# pathway1     1     2     3     4     0     0     0
# pathway2     1     0     0     0     5     0     0
# pathway3     0     0     3     0     0     6     0
The expected output has been printed above, but I wonder if there is a faster way (I mean really faster) to get there because I have a really big matrix to deal with.
Hope someone could give me some help. Many thanks advanced!
 
     
    