num_extract: Extraction of numbers from a character string

View source: R/num_extract.R

num_extractR Documentation

Extraction of numbers from a character string

Description

Simple wrapper for some regex to extract only the numbers from a character string. Source: http://stla.github.io/stlapblog/posts/Numextract.html. There are some other pretty cool functions after the link too.

Usage

num_extract(string, as_char = TRUE)

Arguments

string

A value or vector with mix of numbers and characters

as_char

Logical; if TRUE, returns a character, and if FALSE a numeric.

Value

A vector or value; character or numeric

Examples

num_extract("30.5ml")

pvals <- c('0.00000', '< 0.001', '0.0', '0.123', '0.6', '1', '1.0', '1.000')
num_extract(pvals)
num_extract(pvals, as_char = FALSE)

negatives <- c(-0.133213, -0.06023, -0.004233, -0.000000134234, -1)
num_extract(negatives)

emilelatour/lamisc documentation built on April 9, 2024, 10:33 a.m.