0

Following is the recipe I am creating where I want to convert few numeric features to factors:

    house_recipe <- training(house_split) %>%
      recipe(log_sale_price ~ MSSubClass + OverallCond + LowQualFinSF) %>%
      step_num2factor(
        select(MSSubClass,OverallCond,LowQualFinSF),
          levels = list(fct_cd_mssbclass, fct_cd_ovcond, fct_cd_lwqfinsf)
      )

I get the following error:

Error: Please provide a character vector of appropriate length for levels.

The levels have been passed as list in the code above. The definition of levels is as follows:

#create levels
fct_cd_mssbclass <- as.character(unique(sort(training(house_split)$MSSubClass)))
fct_cd_ovcond <- as.character(unique(sort(training(house_split)$OverallCond)))
fct_cd_lwqfinsf <- as.character(unique(sort(training(house_split)$LowQualFinSF)))

Please advise how to use this step function correctly. I didn't find any similar example in the documentation. Thanks.

massisenergy
  • 1,764
  • 3
  • 14
  • 25
  • 1
    The `levels` argument expects a character vector, not a list. If the three different variables need to have different levels, I believe you'll to use `step_num2factor()` three times, once for each variable. – Julia Silge Apr 13 '20 at 04:21
  • Thanks Julia! Just a suggestion won't it be better design to include all these similar step in one step function? – Ravi Shankar Hela Apr 13 '20 at 19:52
  • I definitely see what you're saying but currently the function isn't quite that flexible. If you would like to open a GitHub issue, we can consider that in our prioritization for future work! – Julia Silge Apr 13 '20 at 23:30
  • Sure, I will.open a GitHub issue. Thank for your response! – Ravi Shankar Hela Apr 14 '20 at 07:25
  • Did you open the issue? Having to use step_num2factor() for each variable is very anti-recipes – GitHunter0 Oct 11 '21 at 22:34

1 Answers1

3

You can factor several variables at once with the function step_mutate_at(). It takes the variables you want to change and any function you want to use: step_mutate_at(MSSubClass,OverallCond,LowQualFinSF, fn = factor)

Manu
  • 31
  • 3
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Jun 01 '20 at 08:53