Extract all numeric values from a string using a regular expression and return a list of all found values. If there are several, the values can be either pasted and/or casted from characters to numeric values.

StrVal(x, paste = FALSE, as.numeric = FALSE, dec = getOption("OutDec"))

Arguments

x

a character vector

paste

should separatetly extracted numbers be pasted together? This can be useful to reverse a prior format action. Default is FALSE.

as.numeric

logical value, determining if the extracted values should be converted to a number or be returned as characters. Default is FALSE.

dec

character string containing a single character. The preferred character to be used as the decimal point. Defaults getOption("OutDec").

Details

If there are multiple numbers in the same string to paste and cast to numeric, pasting will be done first and after pasting the conversion will be performed. So if for example the numbers in x = "34 way 066" should be extracted StrVal(x, paste = TRUE, as.numeric = TRUE) will lead to 34066. This is a useful choice for converting formatted numbers having some kind of bigmark.

Value

depending on the results the function will return either a character vector, in the case every element of x contained only one number, or a list of character vectors containing the found numbers.

Author

Andri Signorell <andri@signorell.net>, Markus Naepflin <markus@naepfl.in> provided an optimized regex

See also

other string functions in DescTools-package, section String functions

Examples

# a simple vector with only one number per element
StrVal(x=c("week 1", "week 3", "week 4", "week 5"))
#> [1] "1" "3" "4" "5"

# several numbers per element, extract each part, do not paste and return characters
StrVal(x=c("This is 1. place: 45.2", "none", "12.1 but -2.7 follow, 10.2e23 "),
       paste = FALSE, as.numeric = FALSE)
#> [[1]]
#> [1] "1"    "45.2"
#> 
#> [[2]]
#> [1] ""
#> 
#> [[3]]
#> [1] "12.1"    "-2.7"    "10.2e23"
#> 

# critical are numbers combined with signs, where we sequentially extract valid numbers
StrVal(x=c("78-23-99", "1e-15-34*789+9", "- 34values"),
       paste = FALSE, as.numeric = FALSE)
#> [[1]]
#> [1] "78"  "-23" "-99"
#> 
#> [[2]]
#> [1] "1e-15" "-34"   "789"   "+9"   
#> 
#> [[3]]
#> [1] "-34"
#> 

# a typical use case for this function is to reverse a previously
#   applied number format

x <- c(100000, 4564654632, -456463)
xf <- Format(x, big.mark="'")

StrVal(xf, paste = TRUE, as.numeric = TRUE)
#> [1]     100000 4564700000    -456463

StrVal(xf, paste = TRUE, as.numeric = FALSE)
#> [1] "100000.0000"  "4.5647e+09"   "-456463.0000"
StrVal(xf, paste = FALSE, as.numeric = TRUE)
#> [[1]]
#> [1] 100   0
#> 
#> [[2]]
#> [1] 4564700000
#> 
#> [[3]]
#> [1] -456  463
#> 
StrVal(xf, paste = FALSE, as.numeric = FALSE)
#> [[1]]
#> [1] "100"      "000.0000"
#> 
#> [[2]]
#> [1] "4.5647e+09"
#> 
#> [[3]]
#> [1] "-456"     "463.0000"
#> 

# use an alternative decimal point
StrVal("8 452,12", dec=",")
#>      [,1]    
#> [1,] "8"     
#> [2,] "452,12"