title: R - how to select elements from sublists of a list by their name
body: <p>I have a list of lists that looks like this:</p>
  <pre><code>list(list(&quot;A[1]&quot; = data.frame(W = 1:5),
            &quot;A[2]&quot; = data.frame(X = 6:10),
            B = data.frame(Y = 11:15),
            C = data.frame(Z = 16:20)),
       list(&quot;A[1]&quot; = data.frame(W = 21:25),
            &quot;A[2]&quot; = data.frame(X = 26:30),
            B = data.frame(Y = 31:35),
            C = data.frame(Z = 36:40)),
       list(&quot;A[1]&quot; = data.frame(W = 41:45),
            &quot;A[2]&quot; = data.frame(X = 46:50),
            B = data.frame(Y = 51:55),
            C = data.frame(Z = 56:60))) -&gt; dflist
  </code></pre>
  <p>I need my output to also be a list of list with length 3 so that each sublist retains elements whose names start with <code>A[</code> while dropping other elements.</p>
  <p>Based on some previous questions, I am trying to use this:</p>
  <pre><code>dflist %&gt;% 
      map(keep, names(.) %in% &quot;A[&quot;)
  </code></pre>
  <p>but that gives the following error:</p>
  <pre><code>Error in probe(.x, .p, ...) : length(.p) == length(.x) is not TRUE
  </code></pre>
  <p>Trying to select a single element, for example just <code>A[1]</code> like this:</p>
  <pre><code>dflist %&gt;% 
      map(keep, names(.) %in% &quot;A[1]&quot;)
  </code></pre>
  <p>also doesn't work. How can I achieve the desired output?</p>