Assume I have this data set. Please let me know if it is a duplicate but I am confused in this.
library(tidymodels)
mt <- mtcars[,c('mpg', 'hp', 'drat', 'am')]
mt$hp <- as.character(mt$hp)
mt$drat <- as.character(mt$drat)
dp_pipe1=recipe(mpg ~ hp + drat + am,data=mt) %>%
update_role(c(hp,
drat),new_role="to_numeric") %>%
step_mutate_at(has_role('to_numeric'), fn= as.numeric)
dp_pipe2=prep(dp_pipe1)
bake(dp_pipe2, NULL)
if you run the last step of bake, you will realise that the value of drat has been changed , in the actual data it was 3.9, 3.9, 3.85 etc but now it is coming like 16, 16, 15 etc. Note I am doing a forced character conversion on mtcars data just to show that I am doing a char to num conversion in the processing of data.
I am sorry if I am mistaken on doc. But unable to understand this. Please help
Note my data has no factors:
EDIT 2:
> glimpse(mt)
Rows: 32
Columns: 4
$ mpg <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3…
$ hp <chr> "110", "110", "93", "110", "175", "105",…
$ drat <chr> "3.9", "3.9", "3.85", "3.08", "3.15", "2…
$ am <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
if I run this:
dp_pipe1=recipe(mpg ~ hp + drat + am,data=mt) %>%
update_role(c(hp,
drat),new_role="to_numeric") %>%
step_mutate_at(has_role('to_numeric'), fn= function(x)as.numeric(as.character(x)))
dp_pipe2=prep(dp_pipe1)
bake(dp_pipe2, NULL)
The code gives right result.
EDIT 1:
I am not sure if it is bug or not, but if we choose
fn = function(x)as.numeric(as.character(x))
in the step_mutate_at, it works fine.