How does one get consistent interval bound formatting when using cut() in R?
For example, the following interval bounds format will vary between 2 and 5 decimal points.
# Sample data
set.seed(1)
data <- runif(600, -1, 1.5)
intervals <- 15
# Create intervals with three decimal points
intervals <- cut(data, seq.int(min(data), max(data), length.out = intervals+1), include.lowest = TRUE)
# Display the intervals
intervals
What I want is the interval bound formatting to be consistent with 3 decimal points. The closest I get is by introducing rounded breaks with 3 decimal points:
rounded_breaks <- round(
seq(min(data), max(data), length.out = intervals + 1),
3)
intervals <- cut(data, breaks = rounded_breaks, include.lowest = TRUE)
Although rounded_breaks holds values with 3 decimal points, cut() seems to drop the third decimal point if it is a 0, therefore rendering the format of the bounds to 2 decimal points.
How can this be adjusted so that the 0 is still shown in the third decimal place of the intervals?
You can extract and re-format the numbers in the labels. Here's one way to do that
We use regular expressions to find the numbers in the labels and use
sprintfto format them with a certain number of decimal places.