This question is realted to this one.
I need a parametrized function to implement the following code:
dt <- data.table( text = c("AAA 123 BBB", "1 CCC")
, text2 = c("AAA 123 BBB", "1 CCC"))
double_number <- function(x) x*2
regex_identify_num <- "\\d+"
dt[, calc_value := text |> str_extract(regex_identify_num) |> as.numeric ()|> double_number()]
dt[, text := mapply(function(x,y) gsub(regex_identify_num, x, y, perl = T), calc_value, text)]
dt[, calc_value:=NULL]
The expected result is to get the numbers doubled in the selected column, as per the code above.
I tried:
change_value <- function(dt_, fun, regex, column_name) {
#browser()
dt_[, new_column := get(column_name) |> str_extract(get(regex)) |> fun()]
dt_[, column_name := mapply(function(x, y) gsub(get(regex), x, y, perl = T), new_column, get(column_name))]
dt_[, new_column := NULL]
return(dt)
}
change_value(dt, function(x) x |> as.numeric ()|> double_number(), "regex_identify_num", "text")
But I get Error in get(column_name) : first argument has length > 1
. Interestingly, when debugging the evaluation of get(column_name) |> str_extract(get(regex)) |> fun()
seems correct (246 2).
How do I fix this?
PS> I also tried a different approach (programming through data.table),no success:
change_value <- function(dt_, fun, regex, column_name) {
#browser()
dt_[, new_column:= column_name|> str_extract(regex) |> fun()
, env = list( fun = substitute(fun)
, column_name = substitute(column_name)
, regex = substitute(regex)
)
]
dt_[, column_name := mapply(function(x, y) gsub(regex, x, y, perl = T), new_column, column_name)]
dt_[, new_column := NULL]
return(dt)
}