convChar2Num: Convert numeric-like characters back to numbers

Description Usage Arguments Value Author(s) Examples

Description

Convert a vector, list or matrix of numeric-like characters back to numeric values. E.g., percentage, currency, scientific numbers or comma-delimited numbers. Non-numeric-like characters are coerced to NA.

Usage

1
2
3
4
5
convChar2Num(x, strict = TRUE, type = c("scientific", "comma",
  "percent", "currency", "wide", "", "e", ",", "%", "$", "_"), ...)

to_num(x, strict = TRUE, type = c("scientific", "comma", "percent",
  "currency", "wide", "", "e", ",", "%", "$", "_"), ...)

Arguments

x

Character in numeric-like values. Either vector, list, matrix or array.

strict

Logical, if only convert strict numeric-like characters, e.g, "4.5%" instead of "about 56%". Default TRUE. If FALSE, it will extract the first numeric- like character parts.

type

Character vector to define which type of numeric-like characters to convert. Default c("scientific", "comma", "percent", "currency", "wide", "", "e", ",", "%", "$", "_"), of which "e", ",", "%", "$" , "_" are short for "scientific", "comma", "percent", "currency", "wide", respectively. If you don't want to apply any conversion, set type="".

Value

Numeric values as in the same strcture as x. Non-numeric-like values will be coerced to NA.

Author(s)

Yiying Wang, wangy@aetna.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
## Not run: 
vec <- c("_84%", "32.9%", "33", "a", " -5", "0.3e2", "1,200", "$67")
to_num(vec)  # return
# [1]     NA   0.329   33.000    NA   -5.000   30.000 1200.000   67.000

to_num(vec, strict=FALSE)  # return
# [1]  0.840   0.329   33.000    NA   -5.000   30.000 1200.000   67.000

to_num('about 56 pounds', strict=FALSE)  # return
# [1]  56

to_num(matrix(vec, nrow=2))  # return
#       [,1] [,2] [,3] [,4]
# [1,]    NA   33   -5 1200
# [2,] 0.329   NA   30   67

to_num(list(list("56%"), list()))  # return

## End(Not run)

madlogos/aseskit documentation built on June 26, 2019, 12:17 a.m.