我有一个 mlr3 任务
df <- data.frame(v1 = c("a", "b", "a"),
v2 = c(1, 2, 2),
data = c(3.15, 4.11, 3.56))
library(mlr3)
task <- TaskRegr$new("bmsp", df, target = "data")
如何在管道中将功能“v1”值“a”重命名为值“c”?
编码:
library(mlr3)
library(mlr3pipelines)
df <- data.frame(v1 = c("a", "b", "a"),
v2 = c(1, 2, 2),
data = c(3.15, 4.11, 3.56))
library(mlr3)
task <- TaskRegr$new("bmsp", df, target = "data")
pop <- po("colapply",
applicator = function(x) ifelse(x == "a", "c", x))
pop$param_set$values$affect_columns = selector_name("v1")
pop$train(list(task))[[1]]$data()
给出输出(见第 v1 列,第 2 行):
data v1 v2
1 3.15 c 1
2 4.11 2 2
3 3.56 c 2
但是需要输出
data v1 v2
1 3.15 c 1
2 4.11 b 2
3 3.56 c 2