Learn R Programming

Ecfun (version 0.3-2)

parseDollars: Convert character string with Dollar signs and commas to numerics

Description

as.numeric of character strings after suppressing commas and dollar signs. This is a special case of parseCommas.

Usage

parseDollars(x, pattern='\\$|,', 
        replacement='', ...)

Value

Numeric vector converted from x.

Arguments

x

vector of character strings to be converted to numerics

pattern

regular expression to be replaced by replacement

replacement

Character string to substitute for each occurrence of pattern

...

optional arguments to pass to gsub

Author

Spencer Graves

Details

as.numeric(gsub(x, ...)). See also parseCommas.

See Also

gsub as.numeric parseCommas

Examples

Run this code
##
## 1.  a character vector
##
X2 <- c('-$2,500', '$5,000.50')
x2 <- parseDollars(X2)

stopifnot(
all.equal(x2, c(-2500, 5000.5))
)

##
## A data.frame
##
chDF <- data.frame(let=letters[1:2], Dol=X2, dol=x2)
numDF <- parseCommas(chDF)

chkDF <- chDF
chkDF$Dol <- x2
stopifnot(
all.equal(numDF, chkDF)
)

Run the code above in your browser using DataLab