tonum {easyr} | R Documentation |
Convert to Number
Description
Flexible number conversion for converting strings to numbers. Handles $ , ' and spaces. Author: Bryce Chamberlain. Tech review: Dominic Dillingham.
Usage
tonum(
x,
preprocessed.values = NULL,
nastrings = easyr::nastrings,
ifna = c("return-unchanged", "error", "warning", "return-na"),
verbose = TRUE,
nazero = FALSE,
checkdate = TRUE,
remove.chars = FALSE,
do.logical = TRUE,
do.try.integer = TRUE,
multipliers = c(`%` = 1/100, K = 1000, M = 1000^2, B = 1000^3)
)
Arguments
x |
Vector to convert. |
preprocessed.values |
Strings need to have NAs set, lowercase and be trimmed before they can be checked. To avoid doing this multiple times, you can pass these processed values to the function. |
nastrings |
Vector of characters to be considered NAs. todate will treat these like NAs. Defaults to the easyr::nastrings list. |
ifna |
Action to take if NAs are created. 'return-unchanged' returns the sent vector unchanged; 'warning' results in a warning and returns the converted vector with new NAs; 'error' results in an error; return-na returns data with new NAs and prints via cat if verbose. |
verbose |
Choose to view messaging. |
nazero |
(Optional) Convert NAs to 0. Defaults to TRUE, if FALSE NAs will stay NA. |
checkdate |
Check if the column is a date first. If this has already been done, set this to FALSE so it doesn't run again. |
remove.chars |
Remove characters for aggressive conversion to numbers. |
do.logical |
Check for logical-form vectors. |
do.try.integer |
Return an integer if possible. Integers are a more compact data type and should be used whenever possible. |
multipliers |
Named vector of factor symbols and values to check. Setting to NULL may speed up operations. |
Value
Converted vector.
Examples
tonum( c('123','$50.02','30%','(300.01)',NA,'-','') )
tonum( c('123','$50.02','30%','(300.01)',NA,'-',''), nazero = FALSE )
tonum( c( '$(3,891)M', '4B', '3.41K', '30', '40K' ) )