str_to_numeric: Extract numbers from strings

Description Usage Arguments Author(s) Examples

View source: R/strings.R

Description

str_to_numeric lets you extract numbers from strings and convert them to a numeric vector. If a single element in the character vector contains multiple numbers, you can specify an aggregate function. See examples.

Usage

1
str_to_numeric(x, FUN = mean, ...)

Arguments

x

A character vector.

FUN

If a string contains several numbers, you can specify an aggregate function. By default this function returns the mean of all numbers, but you can use min, max, median, head or tail also.

...

Arguments passed to FUN.

Author(s)

Kristian D. Olsen

Examples

1
2
3
4
5
x <- c("10", "between 0 and 10", "about 8,5")
y <- str_to_numeric(x, FUN = mean)
identical(y, c(10, 5, 8.5))
y <- str_to_numeric(x, FUN = head, n = 1L)
identical(y, c(10, 0, 8.5))

itsdalmo/reporttoolDT documentation built on May 18, 2019, 7:11 a.m.