Pergunta

I would like to format my negative numbers in "Accounting" format, i.e. with brackets. For example, I would like to format -1000000 as (1,000,000).

I know the way of introducing thousands-separator:

 prettyNum(-1000000, big.mark=",",scientific=F)

However, I am not sure how to introduce the brackets. I would like to be able to apply the formatting to a whole vector, but I would want only the negative numbers to be affected. Not that after introducing the thousands separator, the vector of numbers is now a characater vector, example:

 "-50,000" "50,000"  "-50,000" "-49,979" "-48,778" "-45,279" "-41,321"

Any ideas? Thanks.

Foi útil?

Solução 4

A very easy approach is using paste0 and sub. Here's a simple function for this:

my.format <- function(num){
  ind <- grepl("-", num)
  num[ind] <-  paste0("(", sub("-", "", num[ind]), ")")
  num
}

> num <- c("-50,000", "50,000",  "-50,000", "-49,979", "-48,778", "-45,279", "-41,321")
> my.format(num)
[1] "(50,000)" "50,000"   "(50,000)" "(49,979)" "(48,778)" "(45,279)" "(41,321)"

If you want to reverse the situation, let's say, you have a vector like this:

num2 <- my.format(num)

and you want to replace (·) by -, then try

sub(")", "", sub("\\(", "-", num2))

Outras dicas

Another way, without regex:

x <- c(-50000, 50000, -50000, -49979, -48778, -45279, -41321)
x.comma <- prettyNum(abs(x), big.mark=',')
ifelse(x >= 0, x.comma, paste0('(', x.comma, ')'))
# [1] "(50,000)" "50,000"   "(50,000)" "(49,979)" "(48,778)" "(45,279)" "(41,321)"

You can try my package formattable which has a built-in function accounting to apply accounting format to numeric vector.

> # devtools::install_github("renkun-ken/formattable")
> library(formattable)
> accounting(c(123456,-23456,-789123456))
[1] 123,456.00       (23,456.00)      (789,123,456.00)

You can print the numbers as integers:

> accounting(c(123456,-23456,-789123456), format = "d")
[1] 123,456       (23,456)      (789,123,456)

These numbers work with arithmetic calculations:

> money <- accounting(c(123456,-23456,-789123456), format = "d")
> money
[1] 123,456       (23,456)      (789,123,456)
> money + 5000
[1] 128,456       (18,456)      (789,118,456)

It also works in data.frame printing:

> data.frame(date = as.Date("2015-01-01") + 1:10, 
    balance = accounting(cumsum(rnorm(10, 0, 100000))))
         date      balance
1  2015-01-02  (21,929.80)
2  2015-01-03 (246,927.59)
3  2015-01-04 (156,210.85)
4  2015-01-05 (135,122.80)
5  2015-01-06 (199,713.06)
6  2015-01-07  (91,938.03)
7  2015-01-08  (34,600.47)
8  2015-01-09   147,165.57
9  2015-01-10   180,443.31
10 2015-01-11   251,141.04

Here's an approach that gives the leading spaces:

x <- c(-10000000, -4444, 1, 333)

num <- gsub("^\\s+|\\s+$", "", prettyNum(abs(x), ,big.mark=",", scientific=F))
num[x < 0] <- sprintf("(%s)", num[x < 0])
sprintf(paste0("%0", max(nchar(as.character(num))), "s"), num)


## [1] "(10,000,000)" "     (4,444)" "           1" "         333"

Maybe this?:

a <- prettyNum(-1000000, ,big.mark=",",scientific=F)
paste("(", sub("-", "", a), ")", sep = "")
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top