hughjonesd/santoku

Argument to control number of digits used to format the break number for chop_x functions

abiyug opened this issue · 4 comments

When attempting to chop a vector with chop_xx functions for large numbers, an argument is missing to control the formatted output resulting something like [1e+06]. Where as on base "cut" function, the "dig.lab" argument specify how many digits will be displayed in the output breaks. Here s an example:

  • Vector
    > x <- c(1,10,100,1000,100000, 200000, 1000000, 2000000,3000000)

First, using base 'cut' function

  • cut into three bins with the "dig.lab" set to display 7 digit numbers in the break..
> cut(x, 3, dig.lab =7)
[1] (-2998.999,1000001] (-2998.999,1000001] (-2998.999,1000001]
[4] (-2998.999,1000001] (-2998.999,1000001] (-2998.999,1000001]
[7] (-2998.999,1000001] (1000001,2000000]   (2000000,3003000]  

Now with santoku chop function. I could not find any argument like the "dig.lab" for base::cut, to control digit count.

  • chop evenly into 3 bins
> chop_evenly(x, intervals = 3)
[1] [1, 1e+06)     [1, 1e+06)     [1, 1e+06)     [1, 1e+06)     [1, 1e+06)    
[6] [1, 1e+06)     [1, 1e+06)     [1e+06, 2e+06) [2e+06, 3e+06]
Levels: [1, 1e+06) [1e+06, 2e+06) [2e+06, 3e+06]

Request is to be able to control what number format in the brackets, and have it behave like the base cut.

Use the fmt article to lbl_intervals():

x <- c(1,10,100,1000,100000, 200000, 1000000, 2000000,3000000)
chop_evenly(x, 3, labels = lbl_intervals(fmt = "%.7g"))
[1] [1, 1000001)       [1, 1000001)       [1, 1000001)       [1, 1000001)       [1, 1000001)      
[6] [1, 1000001)       [1, 1000001)       [1000001, 2000000) [2000000, 3000000]
Levels: [1, 1000001) [1000001, 2000000) [2000000, 3000000]

What versio of santoku do you have?

This is what I have.

  • Version
    [1] santoku_0.4.1

chop_evenly(x, 3, labels = lbl_intervals(fmt = "%.7g"))

Error in lbl_intervals(fmt = "%.7g") : unused argument (fmt = "%.7g")

Ok, I restarted R and it works now! I still don't see the fmt argument in the help documentation.

Try reinstalling. The help doc definitely has it:


lbl_intervals             package:santoku              R Documentation

Label chopped intervals using set notation

Description:

     Label chopped intervals using set notation

Usage:

     lbl_intervals(raw = FALSE, fmt = NULL)
     
Arguments:

     raw: Logical. Always use raw ‘breaks’ in labels, rather than e.g.
          quantiles or standard deviations?

     fmt: A format. Can be a string, passed into ‘base::sprintf()’ or
          ‘format()’ methods; or a one-argument formatting function.