我想为tidymodels配方包中的各种步骤函数使用带有列名的向量。我的直觉只是简单地使用(这里所用的prep和juice ):
library(tidymodels)
library(modeldata)
data(biomass)
remove_vector <- c("oxygen","nitrogen")
test_recipe <- recipe(HHV ~ .,data = biomass) %>%
step_rm(remove_vector)
test_recipe %>%
prep %>%
juice %>%
head但这将返回警告:
Note: Using an external vector in selections is ambiguous.
i Use `all_of(remove_vector)` instead of `remove_vector` to silence this message.
i See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
This message is displayed once per session.当然,这关系到我(我想确保我在不遇到错误消息的情况下编写代码),但我仍然得到了我想要的结果。
但是,当我遵循错误消息并在all_of中使用以下内容时
test_recipe <- recipe(HHV ~ .,data = biomass) %>%
step_rm(all_of(remove_vector))
test_recipe %>%
prep %>%
juice %>%
head我得到了错误消息:
错误:步骤函数选择器(例如
all_of)中并不允许所有函数。看到了吗?选择。
在?selections中,我似乎没有找到确切(看似简单)问题的参考。
有什么想法吗?非常感谢!
发布于 2020-05-14 05:38:31
如果使用准引号,就不会收到警告:
library(tidymodels)
library(modeldata)
data(biomass)
remove_vector <- c("oxygen", "nitrogen")
test_recipe <- recipe(HHV ~ .,data = biomass) %>%
step_rm(!!!syms(remove_vector))
test_recipe %>%
prep %>%
juice %>%
head更多关于警告的内容。可能发生的情况是,您将向量命名为与您的列名相同。例如:
oxygen <- c("oxygen","nitrogen")
test_recipe <- recipe(HHV ~ .,data = biomass) %>%
step_rm(oxygen)这将只删除oxygen列。但是,如果使用!!!syms(oxygen),则将删除这两列。
https://stackoverflow.com/questions/61785299
复制相似问题