Reputation: 6805
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
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
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