r-如何从配方中排除字符变量::step_dum()



如何在配方中保持characterID变量PERSON_ID不变?我尝试了update_role(PERSON_ID , new_role = "id variable"),并尝试将其从步骤_摘要step_dummy(all_nominal_predictors(), -all_numeric_predictors(), -all_outcomes(), -has_role(match = "id variable")中排除。它不起作用。它仍然将PERSON_ID转换为因子。有什么建议吗?

这似乎是一个令人困惑的问题。根据配方函数文档,step_factor2string应该将因子转换为字符串。

然而,当你看到食谱时,它会说";fct";用于PERSON_ID。另一方面,如果将strings_as_factors设置为FALSE,则会出现一个错误,说明PERSON_ID不是一个因素:

library(tibble)
library(tidymodels)
data_input <- tibble(target = rep(1,9),
num_var = rep(2,9),
char = c(rep("a", 6),rep("b",3)),
PERSON_ID = as.character(c(rep("W",3),rep("D",6))),
logi = rep(c(TRUE,FALSE,FALSE),3),
fac = as.factor(c(rep("1",6),rep("2",3)))
)

recipe_spec <- recipe(target ~ ., data = data_input) %>% 
update_role("PERSON_ID", new_role = "id variable") %>%
step_dummy(all_nominal_predictors(),-all_numeric_predictors(),-all_outcomes(),-has_role(match = "id variable")) %>% 
step_factor2string(PERSON_ID)
recipe_spec %>%  prep() %>%  juice()  %>%  glimpse()
recipe_spec %>%  prep(strings_as_factors = FALSE) %>%  juice()  %>%  glimpse()


相关内容

  • 没有找到相关文章

最新更新