title: map a matrix content from another matrix R which has not the same length
body: <p>I have a matrix extracted with R which has many NA values ( values not found in the text for extraction)</p>
  
  <p>And i have a reference csv file that contain all combination possibilities of the same data</p>
  
  <p>this is a part of my matrix</p>
  
  <p>data matrix;</p>
  
  <pre><code>       OMIM   GENES_SYMBOL         GENES        CHROMOSOME
  1      (NA)       (arlts1)    (perforin)              (NA)
  2      (NA)          (mtr)          (NA)              (NA)
  3    (325410)         (NA)          (NA)              (NA)
  4      (NA)         (t341c)          (NA)              (5)
  </code></pre>
  
  <p>this is how the csv matrix looks like </p>
  
  <p>dictionary matrix;</p>
  
  <pre><code>  OMIM      GENES_SYMBOL     GENES                               CHROMOSOME
   "612367"   "alpqtl2"  anorexia nervosa,a 1"                        1
   "606788"   "arlts1"    basal cell carcinoma, susceptibility to,      3
   "325410"   "bcc1"     bone mineral density qtl 3                    10
  </code></pre>
  
  <p>I want to map the first matrix with the second one to fill all equivalent values and get rid of <code>NA</code>. the problem is the matrices have not the same length<code>( the second &gt;&gt;&gt;&gt;&gt; the first)</code> and rows in both are not organised the same; <em>the 1st row of the <code>data matrix</code> can be the row number 500 in the <code>dictionary matrix</code></em></p>
  
  <p>I wrote this code but it worked only when 2 matrices have same length. If not it returns only 2 columns from the <code>data matrix</code></p>
  
  <pre><code>genemap&lt;- data.table::fread("GeneMap - Copy.csv",sep="\t")
  
  fun &lt;- function(rowi,genemap) {
    res &lt;- apply(as.data.frame(genemap),1,function(x) {length(na.omit(match(na.omit(rowi),x)))})
    IND &lt;- which(  max(datamatrix) == datamatrixs  )[1]
  
    rowi[is.na(rowi)] &lt;- unlist(genemap[IND,])[is.na(rowi)]
    return(rowi)
  }
  
  as.data.frame(t(apply(datamatrix, 1, fun, genemap))
  )
          OMIM   GENES_SYMBOL       
  1      (NA)       (arlts1)   
  2      (NA)          (mtr)         
  3    (325410)         (NA)         
  4      (NA)        ( t341c)    
  </code></pre>
  
  <p>any suggestion to modify the code??</p>