I have a data table below.
how do I format all columns with variables with comma values?
I know of the scales package but if I use the scales package I won't be able to use the table for some calculating operations any longer
i want something that will still retain the table format type as numeric.
Customers | telex | manay | players |
---|---|---|---|
babs | 3434323424 | 937387573 | 96222221 |
bobs | 53545322 | 758464938 | 122134 |
pint | 43 | 7453537384 | 223444 |
red | 35435 | 624353 | 345654 |
yello | 4567 | 44 | 334 |
I want the output to look like this table below
Customers | telex | manay | players |
---|---|---|---|
babs | 3,434,323,424 | 937,387,573 | 96,222,221 |
bobs | 53,545,322 | 758,464,938 | 122,134 |
pint | 43 | 7,453,537,384 | 223,444 |
red | 35,435 | 624,353 | 345,654 |
yello | 4,567 | 44 | 334 |
CodePudding user response:
You can use formatC
:
library(dplyr)
df <- data.table::fread('Customers telex manay players
babs 3434323424 937387573 96222221
bobs 53545322 758464938 122134
pint 43 7453537384 223444
red 35435 624353 345654
yello 4567 44 334')
df %>%
mutate(across(telex:players, ~as.numeric(.))) %>%
mutate(across(telex:players, ~formatC(., format="d", big.mark=",")))
Which gives:
Customers telex manay players
1: babs NA 937,387,573 96,222,221
2: bobs 53,545,322 758,464,938 122,134
3: pint 43 NA 223,444
4: red 35,435 624,353 345,654
5: yello 4,567 44 334
CodePudding user response:
format() does this.
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 <- df %>%
mutate(
telex = format(telex, big.mark = ",", scientific = F),
manay = format(manay, big.mark = ",", scientific = F),
players = format(players, big.mark = ",", scientific = F)
)
But note that your data changes to type "character".