title: R: use of "where" to select rows by matching an element from a list
body: <p>I'm attempting to use the &quot;where&quot; function in R within a loop to pick out a certain row from two datasets based on a matching element, and then make a scatterplot of the two. They code sample below provides the row names of the two dataframes and the loop. Each dataframe contains more counties than are listed in &quot;mycounties&quot;.</p>
  <p>I have used the &quot;where&quot; function in a loop exactly like this before, but this time R is returning the error message that &quot;where&quot; doesn't exist?  I know &quot;where&quot; exists!  My question is, how do I make R recognize this &quot;where&quot; structure, or is there a better way to pick out the rows I want to plot?</p>
  <pre><code> &gt; names(dts)
   [1] &quot;county&quot;  &quot;Freq125&quot; &quot;Freq126&quot; &quot;Freq127&quot; &quot;Freq128&quot; &quot;Freq129&quot; &quot;Freq130&quot; &quot;Freq131&quot; &quot;Freq132&quot; &quot;Freq133&quot; &quot;Freq134&quot; &quot;Freq135&quot; &quot;Freq136&quot; &quot;Freq137&quot;
  [15] &quot;Freq138&quot; &quot;Freq139&quot; &quot;Freq140&quot; &quot;Freq141&quot; &quot;Freq142&quot; &quot;Freq143&quot; &quot;Freq144&quot; &quot;Freq145&quot; &quot;Freq146&quot; &quot;Freq147&quot; &quot;Freq148&quot; &quot;Freq149&quot; &quot;Freq150&quot; &quot;Freq151&quot;
  [29] &quot;Freq152&quot; &quot;Freq153&quot; &quot;Freq154&quot; &quot;Freq155&quot; &quot;Freq156&quot; &quot;Freq157&quot; &quot;Freq158&quot; &quot;Freq159&quot; &quot;Freq160&quot; &quot;Freq161&quot; &quot;Freq162&quot; &quot;Freq163&quot; &quot;Freq164&quot; &quot;Freq165&quot;
  [43] &quot;Freq166&quot; &quot;Freq167&quot; &quot;Freq168&quot; &quot;Freq169&quot; &quot;Freq170&quot; &quot;Freq171&quot;
  
  &gt; names(pm)
   [1] &quot;county&quot; &quot;pm125m&quot; &quot;pm126m&quot; &quot;pm127m&quot; &quot;pm128m&quot; &quot;pm129m&quot; &quot;pm130m&quot; &quot;pm131m&quot; &quot;pm132m&quot; &quot;pm133m&quot; &quot;pm134m&quot; &quot;pm135m&quot; &quot;pm136m&quot; &quot;pm137m&quot; &quot;pm138m&quot;
  [16] &quot;pm139m&quot; &quot;pm140m&quot; &quot;pm141m&quot; &quot;pm142m&quot; &quot;pm143m&quot; &quot;pm144m&quot; &quot;pm145m&quot; &quot;pm146m&quot; &quot;pm147m&quot; &quot;pm148m&quot; &quot;pm149m&quot; &quot;pm150m&quot; &quot;pm151m&quot; &quot;pm152m&quot; &quot;pm153m&quot;
  [31] &quot;pm154m&quot; &quot;pm155m&quot; &quot;pm156m&quot; &quot;pm157m&quot; &quot;pm158m&quot; &quot;pm159m&quot; &quot;pm160m&quot; &quot;pm161m&quot; &quot;pm162m&quot; &quot;pm163m&quot; &quot;pm164m&quot; &quot;pm165m&quot; &quot;pm166m&quot; &quot;pm167m&quot; &quot;pm168m&quot;
  [46] &quot;pm169m&quot; &quot;pm170m&quot; &quot;pm171m&quot;
  &gt; 
  &gt; mycounties = c(&quot;beaufort&quot;, &quot;bertie&quot;, &quot;bladen&quot;, &quot;camden&quot;, &quot;carteret&quot;, &quot;chowan&quot;, &quot;craven&quot;, 
  +   &quot;cumberland&quot;, &quot;currituck&quot;, &quot;dare&quot;, &quot;duplin&quot;, &quot;gates&quot;, &quot;greene&quot;, &quot;harnett&quot;, &quot;hyde&quot;,
  +   &quot;jones&quot;, &quot;lenoir&quot;, &quot;new hanover&quot;, &quot;onslow&quot;, &quot;pamlico&quot;, &quot;pasquotank&quot;, &quot;pender&quot;, &quot;perquimans&quot;, &quot;pitt&quot;, &quot;robeson&quot;, &quot;sampson&quot;, &quot;tyrrell&quot;, &quot;washington&quot;)
  &gt; 
  &gt; LOOP
  &gt; for (i in 1:length(mycounties)) {
  + x = pm[where(pm$county == mycounties[i]),2:48]
  + y = dt[where(dts$county == mycounties[i]),2:48 ]
  + plot( x, y, main=paste(&quot;HYSPLIT &amp; ED Visits&quot;, counties[i], sep=&quot;&quot;),
  +  xlab=&quot;HYSPLIT&quot;, ylab=&quot;ED Visits&quot;, pch=19)
  + } 
  Error in `[.data.frame`(pm, where(pm$county == mycounties[i]), 2:48) : 
    could not find function &quot;where&quot;
  </code></pre>