Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Displaying numbers as currency in knitr::kable

Tags:

r

I have the following table created using knitr::kable

Week Cost   Disc    total
Wk1 10000   -4000   6000
Wk2 20000   -3000   17000
Wk3 30000   -2000   28000
Wk4 40000   -1000   39000

I would like to display the numbers using an accounting format with a currency. I know you can use the paste function, but I was wondering if there is a cleaner solution. Here is how I would want it to look:

Week Cost      Disc     total
Wk1 $10,000  ($4,000)  $6,000
Wk2 $20,000  ($3,000)  $17,000
Wk3 $30,000  ($2,000)  $28,000
Wk4 $40,000  ($1,000)  $39,000

Any suggestions?

like image 867
Agarp Avatar asked Dec 18 '22 02:12

Agarp


1 Answers

Using currency in formattable library:

##Your sample df

df <- data.frame(Week = c("Wk1", "Wk2", "Wk3", "Wk4"), Cost = c(10000, 20000, 30000, 40000))


library(formattable)
df$Cost <- currency(df$Cost, digits = 0L)
df
#  Week    Cost
#1  Wk1 $10,000
#2  Wk2 $20,000
#3  Wk3 $30,000
#4  Wk4 $40,000
like image 144
Santosh M. Avatar answered Dec 29 '22 00:12

Santosh M.