Learn R Programming

simplevis (version 6.1.0)

cut_format: Convert Numeric to Factor, with custom formatting

Description

This is an enhanced version of [base::cut()] that allows a custom formatting to be applied to the values.

Usage

cut_format(
  x,
  breaks,
  include.lowest = FALSE,
  right = TRUE,
  ordered_result = FALSE,
  ...,
  format_fun = format,
  sep = ", ",
  paren = c("(", "[", ")", "]")
)

Arguments

x

a numeric vector which is to be converted to a factor by cutting.

breaks

`[numeric]` A vector of two or more unique cut points

include.lowest

logical, indicating if an ‘x[i]’ equal to the lowest (or highest, for right = FALSE) ‘breaks’ value should be included.

right

logical, indicating if the intervals should be closed on the right (and open on the left) or vice versa.

ordered_result

logical: should the result be an ordered factor?

...

Passed to `cut()`

format_fun

`[function(x): character]` A vectorized function that performs the desired formatting. Default: [base::format()]

sep

`[character(1)]` The separator between lower and upper end of the interval. Default: `", "`

paren

`[character(4)]` Opening and closing parentheses in two variants. Default: c("(", "[", ")", "]")

References

kimisc package https://github.com/krlmlr/kimisc

Examples

Run this code
# NOT RUN {
library(simplevis)
library(palmerpenguins)
library(dplyr)

penguins %>%
  select(flipper_length_mm) %>%
  mutate(
    flipper_length_mm2 = simplevis:::cut_format(
      flipper_length_mm,
      c(0, 150, 175, 200, Inf),
      right = TRUE,
      include.lowest = TRUE,
      dig.lab = 50,
      ordered_result = TRUE
    )
  ) %>%
  mutate(
    flipper_length_mm3 = simplevis:::cut_format(
      flipper_length_mm,
      c(0, 150, 175, 200, Inf),
      right = TRUE,
      include.lowest = TRUE,
      dig.lab = 50,
      ordered_result = TRUE,
      format_fun = scales::label_dollar(accuracy = 0.1)
    )
  )
# }

Run the code above in your browser using DataLab