我正在尝试在 mutate() + across()
中创建一个函数,将具有五个或更少唯一值(或任意数量)的变量转换为因子,以便稍后使用这些因子进行分组。我认为函数的逻辑是正确的,但我得到了一些不正确的维度错误(西班牙语错误)。为简单起见,我正在使用 mtcars 数据库。
mtcars %>%
mutate(across(1:ncol(.),
function(x) {
if_else(length(unique(x[,i]))<=5,
as.factor(x),
x)}
))
Error: Problem with `mutate()` input `..1`.
i `..1 = across(...)`.
x número incorreto de dimensiones
Run `rlang::last_error()` to see where the error occurred.
非常感谢您的帮助和建议。