R: Check the layouts of numbers.

number_format {validate} | R Documentation

## Check the layouts of numbers.

### Description

Convenience function to check layout of numbers stored as a character vector.

### Usage

    number_format(x, format = NULL, min_dig = NULL, max_dig = NULL, dec = ".")

### Arguments

`x` | `[character]` vector. If `x` is not of type `character` it will be converted.
`format` | `[character]` denoting the number format (see below).
`min_dig` | `[numeric]` minimal number of digits after decimal separator.
`max_dig` | `[numeric]` maximum number of digits after decimal separator.
`dec` | `[character]` decimal seperator.

### Details

If `format` is specified, then `min_dig`, `max_dig` and `dec` are ignored.

Numerical formats can be specified as a sequence of characters. There are a few special characters:

- `d` Stands for digit.

- `*` (digit globbing) zero or more digits

Here are some examples.

`"d.dd"` | One digit, a decimal point followed by two digits.
`"d.ddddddddEdd"` | Scientific notation with eight digits behind the decimal point.
`"0.ddddddddEdd"` | Same, but starting with a zero.
`"d,dd*"` | one digit before the comma and at least two behind it.

### See Also

Other format-checkers: `field_format()`, `field_length()`

### Examples

    df <- data.frame(number = c("12.34","0.23E55","0.98765E12"))
    rules <- validator(
       number_format(number, format="dd.dd")
       , number_format(number, "0.ddEdd")
       , number_format(number, "0.*Edd")
    )

    out <- confront(df, rules)
    values(out)

    # a few examples, without 'validator'
    number_format("12.345", min_dig=2) # TRUE
    number_format("12.345", min_dig=4) # FALSE
    number_format("12.345", max_dig=2) # FALSE
    number_format("12.345", max_dig=5) # TRUE
    number_format("12,345", min_dig=2, max_dig=3, dec=",") # TRUE

[Package validate version 1.1.1 Index]