【问题标题】:tidymodels recipe: Using all_of to select variables stored in a vectortidymodels 配方:使用 all_of 选择存储在向量中的变量
【发布时间】:2020-05-13 21:36:39
【问题描述】:

我想为tidymodels 配方包中的各种步进函数使用带有列名的向量。我的直觉是简单地使用(prepjuice 只是在这里用于说明):

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。

?selections 中,我似乎没有找到对我所遇到的确切(看似简单)问题的参考。

有什么想法吗? 非常感谢!

【问题讨论】:

  • 这不是你的错,因为错误信息令人困惑,但食谱还不能理解所有的 tidyselect 选择器。看起来像你already found what is currently implemented,我们正在努力使这种凝聚力。这肯定不是错误消息的完美组合!
  • 太好了,感谢您确认 Julia!继续做好tidymodels的工作,期待即将到来的! :)

标签: r tidymodels r-recipes


【解决方案1】:

如果您使用 quasiquotation,您将不会收到警告:

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

关于警告的更多信息。您可能会将 vector 命名为与列名之一相同的名称。例如:

oxygen <- c("oxygen","nitrogen")

test_recipe <- recipe(HHV ~ .,data = biomass) %>%
  step_rm(oxygen)

这将仅删除 oxygen 列。但是,如果您使用!!!syms(oxygen),这两列都将被删除。

【讨论】:

  • 这行得通,谢谢!你能评论一下为什么这需要三个!!! - 通常我猜它只是简单的!
  • 我认为您不能使用单个 ! 进行准引用。这是not 运算符。您可以使用 bang bang (!!) 取消引用一个参数,使用 !!! 取消多个参数。更多信息请参见Hadley's book
猜你喜欢
  • 1970-01-01
  • 2021-12-06
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 2014-07-26
相关资源
最近更新 更多