title: Check if vector have same names in a list of names and keep only the names that verify the condition in my vector
body: <p>I have a list of letters called <code>liste</code>, and a vector called <code>myvec</code>, a dataframe and another vector <code>ind1</code> as follow</p>
  <pre><code>###############
  ## my sample ##
  ###############
  liste=LETTERS[seq( from = 1, to = 10 )]
  df1=data.frame(ID1=seq(from=1,to=3,by=1),
                 A=c(rep(1,3)),
                 B=c(rep(2,3)),
                 D=c(rep(3,3)),
                 F=c(rep(4,3)),
                 G=c(rep(5,3)))
  myvec=c(&quot;A&quot;,&quot;B&quot;,&quot;G&quot;)
  ind1=c(1,2,2,3,5,6)
  names(ind1)=c(&quot;ID2&quot;,&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;,&quot;G&quot;)
  
  ind1
  ID2   A   B   C   D   G 
    1   2   2   3   5   6 
  
  df1
    ID1 A B D F G
  1   1 1 2 3 4 5
  2   2 1 2 3 4 5
  3   3 1 2 3 4 5
  
   liste
   &quot;A&quot; &quot;B&quot; &quot;C&quot; &quot;D&quot; &quot;E&quot; &quot;F&quot; &quot;G&quot; &quot;H&quot; &quot;I&quot; &quot;J&quot;
  </code></pre>
  <p>What i want to do is (i figured it out the 3 first checks while writing the post but not the last):</p>
  <p>1- check if names of <code>df1</code> are in <code>myvec</code> : for my example it will give TRUE because of <code> A B G</code>.</p>
  <p>2- check if <code>myvec</code> is contained in <code>liste</code> then i would like to keep in my <code>df1</code> only the subset of names that verify this condition.</p>
  <p>3- do the same 2 checks for my vector <code>ind1</code>.</p>
  <p>I was able to do the 3 checks but not the last one for <code>ind1</code>.</p>
  <p>the expected output will be :</p>
  <pre><code># check 1
  # no problem
   any(myvec %in% names(df1))
   [1] TRUE
  
  # check 2
  # no problem 
  
  if(any(myvec %in% liste)){
    df1=cbind(df1[1],df1[,names(df1) %in% myvec])
  }
    ID1 A B G
  1   1 1 2 5
  2   2 1 2 5
  3   3 1 2 5
  
  # check 3
  # no rpoblem
   any(myvec %in% names(ind1))
  [1] TRUE
  
  # check 4
  # where i got stocked i tried to repeat the same code in check 2 but i got an error for dimension in vector
  # expected output 
  ind1
  ID2   A   B   G 
    1   2   2   6 
  </code></pre>
  <p>Any help will be appreciated or resources about handling names in vectors. Thank you in advance</p>