Suppose we have two special character values ββ(currency):
s1 <- "1.222.333,37" s2 <- "1.223.444,88"
First of all, we want R to display numerical values ββwith the correct number of digits:
Converting a currency to a numerical value can be implemented as follows:
# where s is character moneyToDouble <- function(s){ as.double(gsub("[,]", ".", gsub("[.]", "", s))) } x <- moneyToDouble(s1) + moneyToDouble(s2) x
Printing a numeric value in the form of a currency:
# where x is numeric printMoney <- function(x){ format(x, digits=10, nsmall=2, decimal.mark=",", big.mark=".") } printMoney(x)
source share