-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add thousands separator (function) #60
Comments
For this and #61 have you seen the {scales} package (https://scales.r-lib.org/)? It seems to do both of these things with Looking at the source, it looks like they all ultimately just use |
|
I think the one advantage is there is no need for the input vector to be numeric and so one can apply a thousands separator to strings. > scales::comma("1234")
Error in x * scale : non-numeric argument to binary operator
add_thousands_sep("1234")
[1] "1,234" It's also faster than using x <- sample(1000:10000, size = 10^5, replace = TRUE)
> microbenchmark::microbenchmark(m1 = format(x, big.mark = ","),
+ m2 = scales::comma(x),
+ m3 = add_thousands_sep(x), times = 5)
Unit: milliseconds
expr min lq mean median uq max neval cld
m1 2437.7410 2438.9493 2450.6998 2446.4981 2454.9681 2475.3427 5 b
m2 3143.6610 3152.7269 3397.6765 3156.4771 3172.5286 4362.9889 5 c
m3 65.2607 65.3873 66.9517 65.8287 67.9849 70.2969 5 a The utility is quite niche so I can see why it might be too specific of a function, though I've personally found it helpful 😃 |
Closing this as I no longer think this would be a useful function and one can just use |
Opening this to suggest including a function that adds a thousands separator to numbers.
add_thousands_sep <- function(x){ gsub("(?!^)(?=(?:\\d{3})+$)", ",", x, perl = TRUE) }
This essentially adds a thousands separator for pretty formatting of numbers, including character vectors that contain numbers.
The regex itself could use improvement though but overall handy when some of your numbers are character vectors.
The text was updated successfully, but these errors were encountered: