I have 2 lists:
>logical.list
$mask1
[1] TRUE FALSE TRUE
$mask2
[1] FALSE TRUE FALSE
>work.list
$vector1
[1] "a" "b" "c"
$vector2
[1] "d" "e" "f"
I want to use the first one (logical.list) as a mask to extract the next output from work.list:
>output.list
$vector1
[1] "a" "c"
$vector2
[1] "e"
Is there an elegant decision avoiding loops, lapply, etc.?
CodePudding user response:
Extract with Map
.
Map(`[`, work.list, logical.list)
# $vector1
# [1] "a" "c"
#
# $vector2
# [1] "e"
Data:
logical.list) <- list(mask1 = c(TRUE, FALSE, TRUE), mask2 = c(FALSE, TRUE, FALSE
))
work.list <- list(vector1 = c("a", "b", "c"), vector2 = c("d", "e", "f"))