max04
max04

Reputation: 6805

How to set thousands separator in R?

Anybody knows how to set thousands separator in R?
I would like to get in output sth like that:

123 425 231

or

123.425.231

instead of:

123425231

Thanks.

Upvotes: 3

Views: 9766

Answers (2)

RHertel
RHertel

Reputation: 23788

You can try this:

x <- 123456789101112 
formatC(x, format="f", big.mark = ",", digits=0)
#[1] "123,456,789,101,112"

Of course you can change the entry of "big.mark" as you like, e.g., replace it with a whitespace.

Upvotes: 9

Avinash Raj
Avinash Raj

Reputation: 174706

Through regex,

gsub("(?!^)(?=(?:\\d{3})+$)", ".", '53332', perl=T)
# [1] "53.332"
gsub("(?!^)(?=(?:\\d{3})+$)", ".", '533382', perl=T)
# [1] "533.382"
gsub("(?!^)(?=(?:\\d{3})+$)", ".", '5333829', perl=T)
# [1] "5.333.829"
gsub("(?!^)(?=(?:\\d{3})+$)", ",", '5333829', perl=T)
# [1] "5,333,829"

Upvotes: 5

Related Questions