[SOLVED] Can base:cut() consistently return decimals?

Issue

Unfortunately cut() does not seem to return values with a given number of decimal places (dig.lab doesn’t ensure there are trailing zeros), so I am left with "(2, 2.1]" when I want "(2.0, 2.1]".

I would like to correct this, replacing the first "2" with "2.0", and generally any integer x with "x." followed by a given number of zeros.

If absolutely necessary perhaps someone could suggest a regex for integer (without decimals) x, so that I could replace it myself.

Any help most welcome. Apologies if I am missing something obvious.

Solution

You could use my santoku package:

library(santoku)

chop(rnorm(10), c(-1.55, 0, 1, 1.55), labels = lbl_intervals(fmt = "%.2f"))

 [1] [-1.75, -1.55) [0.00, 1.00)   [0.00, 1.00)   [0.00, 1.00)   [-1.75, -1.55)
 [6] [0.00, 1.00)   [1.00, 1.55)   [-1.55, 0.00)  [-1.55, 0.00)  [1.00, 1.55)  
Levels: [-1.75, -1.55) [-1.55, 0.00) [0.00, 1.00) [1.00, 1.55)

fmt is passed to sprintf. Or you can pass in a formatting function, e.g. scales::label_number(...).

Answered By – dash2

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *