Skip to contents

Extract numeric values from character values

Usage

c_to_n(x, ...)

# S3 method for character
c_to_n(
  x,
  ...,
  pattern_numeric = paste0("^[+-]?",
    "(?:[0-9]+|[0-9]+\\.|\\.[0-9]+|[0-9]+\\.[0-9]+)", "(?:[eE][+-]?[0-9]+)?$"),
  verbose = TRUE
)

# S3 method for factor
c_to_n(x, ...)

# S3 method for numeric
c_to_n(x, ...)

# S3 method for integer
c_to_n(x, ...)

# S3 method for logical
c_to_n(x, ...)

# S3 method for data.frame
c_to_n(x, ..., verbose = TRUE)

Arguments

x

An object to convert from character to numeric using SDTM or ADaM rules.

...

Passed to other `c_to_n()` methods

pattern_numeric

A regular expression to detect numeric values.

verbose

Report detailed status of .

Value

For vectors, a numeric vector with `NA` at locations with non-numeric values. For data.frames and similar, the same class of object with numeric versions of columns added.

Details

The main workhorse function is c_to_n.character, and it will recognize the following numeric types:

  • positive or negative (with optional sign) for all classes

  • integers with an optional decimal point after the number

  • floating point numbers without a number before the decimal point

  • floating point numbers with a number before or after the decimal point

  • scientific notation with any of the prior numeric types as the coefficient and an optional sign before the exponent

Spaces at the beginning and end are removed with `trimws()`

Methods (by class)

  • c_to_n(character): The main workhorse function of c_to_n

  • c_to_n(factor): For factors.

  • c_to_n(numeric): For numeric vectors.

  • c_to_n(integer): For integer vectors.

  • c_to_n(logical): For logical vectors (only handles all-NA case).

  • c_to_n(data.frame): For data.frames and similar, finds columns matching the regular expression pattern `"^..(ST|OR).*C$"`. data.frame method does not replace numeric columns that already exist.