title: R: How can I turn off partial pattern recognition when selecting data frame rows by a vector of required row names?
body: <p>I have been seeing unexpected results when selecting rows of a data frame using a vector of required row names. I've realised this is because R is allowing partial pattern recognition between data frame row names and the stings in my vector. Following the question here .. <a href="https://stackoverflow.com/questions/34233235/r-returning-partial-matching-of-row-names">R returning partial matching of row names</a> .. this seems to be when a string contains characters followed by numerics? The answers in the above question address a single row criteria but do not explain how to deal with row searches as a vector.</p>
  
  <p>For example, if I have a data frame (df):</p>
  
  <pre><code>df&lt;-data.frame(matrix(c(0.5,0.4,0.6,rep(0,3)), ncol=2, nrow=3))
  colnames(df)&lt;-c("pdx","primary")
  rownames(df)&lt;-c("chr6_LINC00680-GUSBP4","chr6_MIR5689HG","chr1_SPRR2")
  
  &gt; df
                            pdx primary
  chr6_LINC00680-GUSBP4     0.5       0
  chr6_MIR5689HG            0.4       0
  chr1_SPRR2                0.6       0
  </code></pre>
  
  <p>And a search vector (test_vector):</p>
  
  <pre><code>test_vector&lt;-c("chr6_MIR5689","chr6_LINC00680","chr1_SPRR2")
  
  &gt; test_vector
  [1] "chr6_MIR5689"   "chr6_LINC00680" "chr1_SPRR2" 
  </code></pre>
  
  <p>If I search for the values of column "pdx" matching the rows in the search vector I get:</p>
  
  <pre><code>&gt; df[test_vector,"pdx"]
  [1] 0.4 0.5 0.6
  </code></pre>
  
  <p>Or by all columns, I get:</p>
  
  <pre><code>&gt; df[test_vector,]
                            pdx primary
  chr6_MIR5689HG            0.4       0
  chr6_LINC00680-GUSBP4     0.5       0
  chr1_SPRR2                0.6       0
  </code></pre>
  
  <p>If an exact match is present in the row names this doesn't occur:</p>
  
  <pre><code>df2&lt;-data.frame(matrix(c(0.6,10,20,0.5,0.4,rep(0,5)), ncol=2, nrow=))
  colnames(df2)&lt;-c("pdx","primary")
  rownames(df2)&lt;-c("chr1_SPRR2C","chr6_LINC00680","chr6_MIR5689","chr6_LINC00680-GUSBP4","chr6_MIR5689HG")
  
  &gt; df2
                         pdx primary
  chr1_SPRR2C            0.6       0
  chr6_LINC00680        10.0       0
  chr6_MIR5689          20.0       0
  chr6_LINC00680-GUSBP4  0.5       0
  chr6_MIR5689HG         0.4       0
  
  &gt; df2[test_vector,]
                  pdx primary
  chr6_MIR5689   20.0       0
  chr6_LINC00680 10.0       0
  chr1_SPRR2C     0.6       0
  </code></pre>
  
  <p>I am extracting values from a data frame using a df[row-vector,column] match, where not all row names I'm searching for are present in the data frame. I need to retain this information as an NA, with matches/NAs in the same order as the initial search vector.</p>
  
  <p>So ideally I would get:</p>
  
  <pre><code>&gt; df[test_vector,"pdx"]
  [1] NA NA 0.6
  </code></pre>
  
  <p>How can I get around this partial pattern recognition, while retaining the output in the same order as the search vector, with a search vector of ~ 10,000 elements, avoiding loops and with any elements in the vector not present in rownames(df) replaced by NA?</p>
  
  <p>(Being ran with version.string R version 3.6.1 (2019-07-05))</p>