使用向量元素与命名列表相交

时间:2019-06-04 13:13:18

标签: r

我具有以下元素向量:

list_alg <- c("rf", "nnet", "knn", "rpart")

,我希望将其与以下命名列表相交:

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")
)

我尝试了intersect(list_alg , list_tune)的变体;但是,这将返回一个空列表。我的目标是创建包含list_alg向量元素的列表;但是也会保留list_tune的其他元素(请参见下面的示例输出)。

我试图实现的输出看起来像这样:

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"

0 个答案:

没有答案