title: Remove and set aside elements of vectors within a list that don't exist in another vector
body: <p>I'm trying to form arguments for use in the <code>reshape()</code> function.  I have a vector of column names, some of which should be merged by <code>reshape()</code> because they share the same letter at the end:</p>
  
  <pre><code>&gt; v &lt;- c("x","da","db","ea","eb","ec","fb")
  </code></pre>
  
  <p>Most of these columns are comprised of a combination of <code>pre</code> and <code>post</code> characters.  <code>pre</code> will be the <code>timevar</code> argument and <code>post</code> will be the <code>v.names</code> argument in <code>reshape()</code>.  They are defined as:</p>
  
  <pre><code>&gt; pre &lt;- c("d","e","f")
  &gt; post &lt;- c("a","b","c")
  </code></pre>
  
  <p>I have organized the problem this way since there are a variable number of columns I will have to perform this on for different files.  By parsing the column names like this, I'm sure I can do this with an algorithm rather than a manual hack.</p>
  
  <p>My desired output is a list of vectors that only include elements of <code>v</code> that share the same <code>post</code> letter.  The intention is to use these as the <code>varying</code> parameter in <code>reshape()</code>:</p>
  
  <pre><code>&gt; desired_lov
  $a
  [1] "da" "ea"
  
  $b
  [1] "db" "eb" "fb"
  </code></pre>
  
  <p>And in addition, I would like to keep track of which elements are missing from <code>desired_lov</code> which still exist in the original <code>v</code> vector.  The intention is to use these as the <code>idvar</code> parameter in <code>reshape()</code>:</p>
  
  <pre><code>&gt; desired_idh
  [1] "x" "ec"
  </code></pre>
  
  <p>With all that given, someone helped me to build a list of vectors with possible column names with those prefixes and postfixes.  Each vector in this list is named after an element in <code>post</code>, and I believe this is important in order for this to work with <code>reshape()</code> since it will merge those columns in each vector under a common name:</p>
  
  <pre><code>&gt; lov &lt;- Map(function(x) paste0(pre,x),post)
  &gt; lov
  $a
  [1] "da" "ea" "fa"
  
  $b
  [1] "db" "eb" "fb"
  
  $c
  [1] "dc" "ec" "fc"
  </code></pre>
  
  <p>Except this builds more names from those combinations than actually exist in <code>v</code>.  So I would like to keep track of which names in <code>v</code> do not exist in <code>lov</code>, for which I've tried:</p>
  
  <pre><code>&gt; idh &lt;- NULL
  &gt; Map(function(x) idh &lt;- paste(idh,lov[[x]][lov[[x]] %in% v]),1:length(lov))
  [[1]]
  [1] " da" " ea"
  
  [[2]]
  [1] " db" " eb" " fb"
  
  [[3]]
  [1] " ec"
  
  &gt; idh
  NULL
  </code></pre>
  
  <p>Except apparently I'm not succeeding in modifying the <code>idh</code> variable using <code>Map()</code></p>
  
  <p>For the next step (after I figure out the bit immediately above), in order to strip out the elements of <code>lov</code> that don't match <code>v</code>, I've tried:</p>
  
  <pre><code>&gt;  Map(function(x) lov[[x]] &lt;- lov[[x]][lov[[x]] %in% v],1:length(lov))
  [[1]]
  [1] "da" "ea"
  
  [[2]]
  [1] "db" "eb" "fb"
  
  [[3]]
  [1] "ec"
  
  &gt; lov
  $a
  [1] "da" "ea" "fa"
  
  $b
  [1] "db" "eb" "fb"
  
  $c
  [1] "dc" "ec" "fc"
  </code></pre>
  
  <p>Which gives me promising output (I would need to remove all vectors from that list that have length &lt; 2 since I'm only looking for duplicated columns based on their second characters), but once again it failed to actually modify <code>lov</code> by removing the elements I was trying to remove.</p>
  
  <p>I've tried searching, but all I keep finding are ways to remove elements of vectors.  This seems to be a much different problem since I'm trying to remove elements from multiple vectors embedded in a list while trying to preserve the vector names in that list.</p>
  
  <p>Edit: I do know about <code>x</code> ahead of time, so I can manually exclude it where needed.  But I don't know that <code>c</code> is a unique postfix ahead of time (in this particular example), so it needs to be determined within the script.</p>