title: How Can I vectorize this function to return an index vector?
body: <p>I'm new to R and am trying to get a handle on the apply family of functions.  Specifically, I am trying to write a higher-order function that will accept 2 character vectors, "host", and "guest" (which do not need to be the same length) and return me an index vector the same length as "host", with the resulting elements corresponding to their indices in guest (NA if not there).</p>
  
  <pre><code>host &lt;- c("A","B","C","D")
  guest &lt;- c("D","C","A","F")
  
  matchIndices &lt;- function(x,y)
  {
    return(match(x,y))
  }
  </code></pre>
  
  <p>This code returns 3 as expected:</p>
  
  <pre><code>matchIndices(host[1],guest)
  </code></pre>
  
  <p>This is the loop I'd like to be able to replace with a succinct apply function (sapply?)</p>
  
  <pre><code>for (i in 1:length(host)) 
  { idx &lt;- matchIndices(host[i],guest); 
    cat(paste(idx,host[i],"\n",sep=";"))
  }
  </code></pre>
  
  <p>This code "works" in that it produces the output below, but I really want the result to be a vector, and I have a hunch that one of the apply functions will do the trick.  I'm just stuck on how to write it.  Any help would be most appreciated.  Thanks.</p>
  
  <p>3;A;
  NA;B;
  2;C;
  1;D;</p>