Consider this simple list
list('a' = list(1,2),'b'=list(list(3,4), list(5,6)), 'c' = 'hello')
$a
$a[[1]]
[1] 1
$a[[2]]
[1] 2
$b
$b[[1]]
$b[[1]][[1]]
[1] 3
$b[[1]][[2]]
[1] 4
$b[[2]]
$b[[2]][[1]]
[1] 5
$b[[2]][[2]]
[1] 6
$c
[1] "hello"
Let's say I want to keep the element a
and b
from it. I do not understand why this works
list('a' = list(1,2),'b'=list(list(3,4), list(5,6)), 'c' = 'hello') %>%
purrr::keep(names(.) == 'a' | names(.) == 'b')
while using the following does not work
> list('a' = list(1,2),'b'=list(list(3,4), list(5,6)), 'c' = 'hello') %>%
+ purrr::keep(~names(.x) %in% c('a','b'))
Error: Predicate functions must return a single `TRUE` or `FALSE`, not a logical vector of length 0
Call `rlang::last_error()` to see a backtrace
what is wrong with the second syntax?
Thanks!