step_mutate or step_mutate_at using custom function

Hi there. I am trying to create a recipe with a step function that takes the input character variable and bins it into various categories depending on the value of the variable. For example

     if(str_detect(word, "a")) return("A")
     if(str_detect(word, "b")) return("B")

main_rec <- recipe(y ~ word,  df) |>
      step_mutate_at(word, fn = list(my_ custom_function))

# OR

main_rec <- recipe(y ~ word,  df) |>
      step_mutate(word =  as.factor(map_chr(word, .f = my_custom_function)))

It throws an error either not recognizing the custom function ot map_chr or str_detect.

What am I doing wrong?

Any help is appreciated.

Using a mapping function inside of step_mutate() might be pushing the boundaries of what it can do (esp in parallel).

You could change your function to a case_when() and achieve the same thing. There is also step_regex(); depending on what you do with the return values, that might solve it for you.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.