Reputation: 1232
I am looking to format Quote dollars number to a currency format within dplyr verbs with no cents included. Rounded up to the nearest dollar. Here is the code I have so far and its output. How can I get quote dollars to go from a number (1000.5 to $1001)?
$ ADD_PROD_DESC <chr> NA, NA, "Copper White 147-1472G", "Copper White 147-1472G", "Copper White 147-1472G", "Copper White 147-1472G", "Copper White 147-1472G", "Copper White~
$ Manufacturer_Model_NBR <chr> NA, "1404N42-00", "147-1472G", "147-1472G", "147-1472G", "147-1472G", "147-1472G", "147-1472G", "147-1472G", "147-1672G", "147-1672G", "147-1672G", "14~
$ Call_For_Price <chr> "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "FALSE", "TRUE", "TRUE~
$ Quote_Dollars <dbl> 1784781.3, 1008000.0, 746010.0, 703822.5, 703822.5, 703822.5, 703822.5, 646818.5, 646818.5, 613302.3, 560757.6, 560757.6, 560757.6, 560757.6, 519990.0,~
Upvotes: 3
Views: 4325
Reputation: 2906
You can use the package scales
:
Quote_Dollars <- scales::dollar_format()(data$Quote_Dollars)
dollar_format
round to the closest by default.
Upvotes: 5
Reputation: 26218
You may do like this in dplyr
pipes. But you have to be careful to do this in last of your pipe, as it/these will convert numeric
to string
and you won't be able to perform further calculations on these.
set.seed(123)
Quote_Dollars <- sample(7000:20000, 5)/7
df <- data.frame(col1 = LETTERS[1:5],
col2 = letters[6:10],
Quote_Dollars = Quote_Dollars)
df
#> col1 col2 Quote_Dollars
#> 1 A f 1351.714
#> 2 B g 1358.571
#> 3 C h 2488.286
#> 4 D i 2245.286
#> 5 E j 2783.143
library(dplyr, warn.conflicts = F)
df %>% mutate(Quote_Dollars = paste0('$', round(Quote_Dollars,0)))
#> col1 col2 Quote_Dollars
#> 1 A f $1352
#> 2 B g $1359
#> 3 C h $2488
#> 4 D i $2245
#> 5 E j $2783
As suggested by MonJeanJean, this should also work.
df %>% mutate(Quote_Dollars = scales::dollar(Quote_Dollars, largest_with_cents = 0))
col1 col2 Quote_Dollars
1 A f $1,352
2 B g $1,359
3 C h $2,488
4 D i $2,245
5 E j $2,783
Upvotes: 4