I wrote a function that transforms a string representing a number (magrittr loaded in my system):
adjust_perc_format <- function(x, n=3){
gsub(",", ".", x, perl = T) %>% as.numeric() %>% format(nsmall=n, decimal.mark = ",")
}
So that:
adjust_perc_format("2,5", 3)
[1] "2,500"
The goal is to transform the ocurrences defined by a regex within a string (see here). For this purpose I tried gsubfn:
str <- "20MG/ML (2,5%)+0,5%"
gsubfn("[\\d,]+(?=%)", function(x) adjust_perc_format(x, n=3),str)
The expected result is "20MG/ML (2,500%)+0,500%"
. Instead, I got the same input string ("20MG/ML (2,5%)+0,5%"
).
I also tried to set the engine as below, no success:
options(gsubfn.engine = "R")
What am I missing here? Thank you.