在使用tidyverse包中的选择函数时,我已经开始收到警告。
示例:
library(dplyr)
set.seed(123)
df = data.frame(
"id" = c(rep("G1", 3), rep("G2", 4), rep("G3", 3)),
"total" = sample.int(n = 10),
"C1" = sample.int(n=10),
"C2" = sample.int(n=10),
"C3" = sample.int(n=10))
cols.to.sum = c("C1", "C2")
df.selected = df %>%
dplyr::select(total, cols.to.sum)给予:
Note: Using an external vector in selections is ambiguous.
i Use `all_of(cols.to.sum)` instead of `cols.to.sum` to silence this message.
i See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
This message is displayed once per session.如果我重构为:
df.selected = df %>%
dplyr::select(total, all_of(cols.to.sum))这种行为已经从tidyselect_0.2.5变成了tidyselect_1.0.0。直到现在才有任何警告。
在有关此更改(https://tidyselect.r-lib.org/reference/faq-external-vector.html)的文档中,有人指出这只是一个警告,但将来它会变成一个错误。
我在这里的问题是如何处理对现有代码的这种更改。
是否应该重构使用此选择方法将all_of()添加到外部向量引用的每一行代码?这听起来很难实现,因为代码中可能有数百个这样的选择(例如,它也会影响到其他函数,例如summarise_at )。
唯一的选择是坚持使用tidyselect_0.2.5来保持代码运行吗?
在关于现有代码的包中进行这样的更改有什么方法呢?
谢谢
发布于 2021-05-26 19:19:36
如果应该是您第一个问题中的操作短语,那么可能只是确保您的变量中没有一个被命名为cols.to.sum。只要是这样,使用all_of()的属性就不会与您的用例相关,您可以像往常一样保持select运行。
如果您不想继续使用旧版本的tidyselect,则抑制库可能会有所帮助
https://stackoverflow.com/questions/61026514
复制相似问题