问题描述
似乎是一个基本问题,也许我只是遗漏了一些明显的东西......但是有没有办法pluck
一个子列表(使用purrr
)?更具体地说,这是一个初始列表:
Seems like a basic question and perhaps I'm just missing something obvious ... but is there any way to pluck
a sublist (with purrr
)?More specifically, here's an initial list:
l <- list(a = "foo", b = "bar", c = "baz")
我想返回一个只有元素 a
和 b
的新(子)列表.通常,我只会做基础"R 子列表:
And I want to return a new (sub-)list with only elements a
and b
.Normally, I'd just do 'base' R sub-listing:
l[c("a", "b")]
但这并没有提供 pluck
的良好 .default
处理.我的理解是 pluck 'replaces' [[
,但是是否有一个 purrr
等价物来替换 [
?
But this doesn't provide the nice .default
handling of pluck
.My understanding is that pluck 'replaces' [[
, but is there a purrr
equivalent for replacing [
?
推荐答案
类似于 jzadra 的帖子,你也可以这样做:
Similar to jzadra's post, you could also do:
l %>% keep(names(.) %in% c("a","b"))
这篇关于是否存在多值 purrr::pluck?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!