-3

I'm using 'data.table' package and trying to use the formatting prints functions of the package. I want to present some of the columns as Currency and some as Percentage, But I got an error when specifying the table column names. my data :

str(dt) :

str(dt):

when trying to apply:

dt %>% formatCurrency(c('A','E'))

"Error in name2int(name, names, rownames) : You specified the columns: A, E, but the column names of the data are "

edit: this issue can be re-created by:

tmp = data.table('A' = rep(1:10),'B' = rep(11:20))

tmp %>% formatPercentage(c('A','B'),2)
Jaap
  • 81,064
  • 34
  • 182
  • 193
  • Please provide a plain text example using `dput` for example. It will be easier for SO community to help you – Emmanuel-Lin Jul 03 '18 at 12:22
  • This is my first post so apologies if it's a bit messy. To be honest I never heard about dput. I added a 2 line code which is re-creating the issue I mentioned, do you think it is neccersy to add more than that? I feel like this isn't a very specfic case (in terms of the kind of table or code I'm running) – eden anteby Jul 03 '18 at 12:45
  • 3
    This is how to provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – VicaYang Jul 03 '18 at 13:13
  • 1
    I get the same error with a data.frame. From `?formatCurrency` the opject it accepts is a "*a table object created from datatable()*" which is not the same as a `data.table` or a `data.frame`. In either way, it is not data.table related error. You need to read the docs. Also, you need to specify that `formatCurrency` is from the `DT` package. – David Arenburg Jul 03 '18 at 14:02
  • Yes, `DT::formatCurrency` is a wrapper of JS code for formatting. It does not work on data.table directly. The error message could be improved to make this clearer. – smci Jul 03 '18 at 16:20

1 Answers1

1

You seem to be mixing packages... formatPercentage() is from DT, you make the data with data.table and then use %>% from dplyr

Why not do it all under the tidyverse / dplyr umbrella?

library(tidyverse)

tmp <- data.frame(A = rep(1:10), B = rep(11:20))

tmp %>% mutate(A = scales::percent(A), B = scales::dollar(B))

you can change the currency of dollar() using dollar_format()

Shinobi_Atobe
  • 1,793
  • 1
  • 18
  • 35
  • Thanks! Your comment led me to my mistake- using 'data.table' instead of 'datatable'. the pipe & the formatPercentage() works well when creating the table : ' tmp <- datatable(A = rep(1:10), B = rep(11:20)) ' ' tmp %>% formatPercentage(c('A','B'),2) ' – eden anteby Jul 04 '18 at 10:16