You can use the scales::label_comma()
function. Note that the format for using this is scales::label_comma()(x)
since a label function is generated and then you call it on your numerical vector.
As noted in similar answers above, this converts the column type to character. To convert back to number, you can use readr::parse_number
.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df <- tibble(
Customers = c("babs", "bobs", "pint", "red", "yellow"),
telex = c(3434323424, 53545322, 43, 35435, 4567),
manay = c(937387573, 758464938, 7453537384, 624353, 44),
players = c(96222221, 122134, 223444, 345654, 334)
)
df
#> # A tibble: 5 x 4
#> Customers telex manay players
#> <chr> <dbl> <dbl> <dbl>
#> 1 babs 3434323424 937387573 96222221
#> 2 bobs 53545322 758464938 122134
#> 3 pint 43 7453537384 223444
#> 4 red 35435 624353 345654
#> 5 yellow 4567 44 334
df_with_comma <- df %>%
mutate(across(telex:players, scales::label_comma()))
df_with_comma
#> # A tibble: 5 x 4
#> Customers telex manay players
#> <chr> <chr> <chr> <chr>
#> 1 babs 3,434,323,424 937,387,573 96,222,221
#> 2 bobs 53,545,322 758,464,938 122,134
#> 3 pint 43 7,453,537,384 223,444
#> 4 red 35,435 624,353 345,654
#> 5 yellow 4,567 44 334
df_reverted <- df_with_comma %>%
mutate(across(telex:players, readr::parse_number))
df_reverted
#> # A tibble: 5 x 4
#> Customers telex manay players
#> <chr> <dbl> <dbl> <dbl>
#> 1 babs 3434323424 937387573 96222221
#> 2 bobs 53545322 758464938 122134
#> 3 pint 43 7453537384 223444
#> 4 red 35435 624353 345654
#> 5 yellow 4567 44 334
Created on 2022-06-07 by the reprex package (v2.0.1)