I have the following vector of elements:
list_alg <- c("rf", "nnet", "knn", "rpart")
and I wish to intersect it with the following named list:
list_tune <- list(
glmnet = caretModelSpec(method = "glmnet"),
svm = caretModelSpec(method = "svmRadial"),
rf = caretModelSpec(method = "rf", importance = TRUE),
nnet = caretModelSpec(method = "nnet"),
knn = caretModelSpec(method = "knn"),
rpart = caretModelSpec(method = "rpart")
)
I've tried variations of intersect(list_alg , list_tune)
; however this returns an empty list. My goal is to create list that includes elements of list_alg
vector; however also keeps the other elements of list_tune
(Please see example output below).
The output I am trying to achieve would look like this:
list(rf = list(method = "rf", importance = TRUE), nnet = list(
method = "nnet"), knn = list(method = "knn"), rpart = list(
method = "rpart"))
$`rf`
$`rf`$`method`
[1] "rf"
$`rf`$importance
[1] TRUE
$nnet
$nnet$`method`
[1] "nnet"
$knn
$knn$`method`
[1] "knn"
$rpart
$rpart$`method`
[1] "rpart"