extract_num {clinpubr}R Documentation

Extract numbers from string.

Description

Extract numerical values from strings. Can be used to filter out the unwanted information coming along with the numbers.

Usage

extract_num(
  x,
  res_type = c("first", "range"),
  multimatch2na = FALSE,
  leq_1 = FALSE,
  allow_neg = TRUE,
  zero_regexp = NULL,
  max_regexp = NULL,
  max_quantile = 0.95
)

Arguments

x

A character vector.

res_type

The type of the result. Can be "first" or "range". If "first", the first number in the string is extracted. If "range", the mean of the range in the string is extracted.

multimatch2na

If TRUE, multiple matches will be converted to NA. Only works when res_type is "first".

leq_1

If TRUE, numbers greater than 1 will be converted to NA. Only works when res_type is "first".

allow_neg

If TRUE, negative numbers are allowed. Otherwise, only positive numbers are allowed.

zero_regexp

A regular expression to match the string that indicates zero.

max_regexp

A regular expression to match the string that indicates the maximum value.

max_quantile

The quantile of values to set the maximum value to.

Details

The function uses regular expressions to extract numbers from strings. The regular expression used is "-?[0-9]+\\.?[0-9]*|-?\\.[0-9]+", which matches any number that may have a decimal point and may have a negative sign.

Value

A numeric vector.

Examples

x <- c("1.2(XXX)", "5-8POS", "NS", "FULL", "5.5", "4.2")
extract_num(x)
extract_num(x,
  res_type = "first", multimatch2na = TRUE, zero_regexp = "NEG|NS",
  max_regexp = "FULL"
)
extract_num(x, res_type = "range", allow_neg = FALSE, zero_regexp = "NEG|NS", max_regexp = "FULL")

[Package clinpubr version 1.0.1 Index]